dataシートの一列目から
列から選択する。
フォーム1にコンボボックス1から7にセットする
icoが列
複数のコンボボックスの絞込抽出(7個)
Dim ITE As Variant
Dim flg As Variant
Dim key As String '文字
Dim key1 As String '文字
Dim key2 As Integer '数字
Dim key3 As Integer '数字
Dim key4 As String '英字
Dim ichi As String '文字
Dim ichi1 As Integer '数字
Dim ichi2 As String '文字
Dim I As Integer
Private Sub UserForm_Initialize()
Dim ico As Long
ico = 1
With ThisWorkbook.Worksheets("data")
Do While .Cells(ico, 1) <> ""
ITE = .Cells(ico, 1).Value
flg = 0
For I = 0 To Me.ComboBox1.ListCount - 1
If ITE = Me.ComboBox1.List(I) Then flg = 1
Next I
If flg = 0 Then Me.ComboBox1.AddItem ITE
ico = ico + 1
Loop
End With
Dim ico2 As Long
ico2 = 1
With ThisWorkbook.Worksheets("data")
Do While .Cells(ico2, 6) <> ""
ITE = .Cells(ico2, 6).Value
flg = 0
For I = 0 To Me.ComboBox6.ListCount - 1
If ITE = Me.ComboBox6.List(I) Then flg = 1
Next I
If flg = 0 Then Me.ComboBox6.AddItem ITE
ico2 = ico2 + 1
Loop
End With
Me.ComboBox1.SetFocus
End Sub
Private Sub ComboBox1_Change()
'ComboBox2セット
Dim ico As Long
ico = 1 '読み込みY座標
With ThisWorkbook.Worksheets("data")
key = Me.ComboBox1.Text '1つ前の値をキーにする
Me.ComboBox2.Clear 'コンボボックスクリア
Do While .Cells(ico, 1) <> "" 'リストの最後までループ
If .Cells(ico, 1) = key Then 'A列がキーの値だったら
ITE = .Cells(ico, 2).Value 'B列の値をITEに
flg = 0 '追加フラグ
For I = 0 To Me.ComboBox2.ListCount - 1 'コンボボックス2をループ
If ITE = Me.ComboBox2.List(I) Then flg = 1 'ITEの値がすでにコンボボックス2に入っている
Next
If flg = 0 Then Me.ComboBox2.AddItem ITE 'FLGが0だったらITEをコンボボックスに追加
End If
ico = ico + 1 '読み込みY座標+1
Loop
End With
Me.ComboBox2.SetFocus
End Sub
Private Sub ComboBox2_Change()
'ComboBox3セット
Dim ico As Long
ico = 1
With ThisWorkbook.Worksheets("data")
key = Me.ComboBox1.Text
key1 = Me.ComboBox2.Text
Me.ComboBox3.Clear
Do While .Cells(ico, 1) <> ""
If .Cells(ico, 1) = key And .Cells(ico, 2) = key1 Then
ITE = .Cells(ico, 3).Value
flg = 0
For I = 0 To Me.ComboBox3.ListCount - 1
If ITE = Me.ComboBox3.List(I) Then flg = 1
Next
If flg = 0 Then Me.ComboBox3.AddItem ITE
End If
ico = ico + 1
Loop
End With
Me.ComboBox3.SetFocus
End Sub
Private Sub ComboBox3_Change()
'ComboBox4セット
Dim ico As Long
ico = 1
With ThisWorkbook.Worksheets("data")
key = Me.ComboBox1.Text
key1 = Me.ComboBox2.Text
key2 = Me.ComboBox3.Text
key3 = CInt(combobox3text)
Me.ComboBox4.Clear
Do While .Cells(ico, 1) <> ""
If .Cells(ico, 1) = key And .Cells(ico, 2) = key1 And .Cells(ico, 3) = key2 Then
ITE = .Cells(ico, 4).Value
flg = 0
For I = 0 To Me.ComboBox4.ListCount - 1
If ITE = Me.ComboBox4.List(I) Then flg = 1
Next
If flg = 0 Then Me.ComboBox4.AddItem ITE
End If
ico = ico + 1
Loop
End With
Me.ComboBox4.SetFocus
End Sub
Private Sub ComboBox4_Change()
'ComboBox5セット
Dim ico As Long
ico = 1
With ThisWorkbook.Worksheets("data")
key = Me.ComboBox1.Text
key1 = Me.ComboBox2.Text
key2 = Me.ComboBox3.Text
key3 = Me.ComboBox4.Text
Me.ComboBox5.Clear
Do While .Cells(ico, 1) <> ""
If .Cells(ico, 1) = key And .Cells(ico, 2) = key1 And .Cells(ico, 3) = key2 And .Cells(ico, 4) = key3 Then
ITE = .Cells(ico, 5).Value
flg = 0
For I = 0 To Me.ComboBox5.ListCount - 1
If ITE = Me.ComboBox5.List(I) Then flg = 1
Next
If flg = 0 Then Me.ComboBox5.AddItem ITE
End If
ico = ico + 1
Loop
End With
Me.ComboBox5.SetFocus
End Sub
Private Sub ComboBox6_Change()
'ComboBox7セット
Dim ico2 As Long
ico2 = 1 '読み込みY座標
With ThisWorkbook.Worksheets("data")
ichi = Me.ComboBox6.Text '1つ前の値をキーにする
Me.ComboBox7.Clear 'コンボボックスクリア
Do While .Cells(ico2, 6) <> "" 'リストの最後までループ
If .Cells(ico2, 6) = ichi Then 'A列がキーの値だったら
ITE = .Cells(ico2, 7).Value 'B列の値をITEに
flg = 0 '追加フラグ
For I = 0 To Me.ComboBox7.ListCount - 1 'コンボボックス2をループ
If ITE = Me.ComboBox7.List(I) Then flg = 1 'ITEの値がすでにコンボボックス2に入っている
Next
If flg = 0 Then Me.ComboBox7.AddItem ITE 'FLGが0だったらITEをコンボボックスに追加
End If
ico2 = ico2 + 1 '読み込みY座標+1
Loop
End With
Me.ComboBox7.SetFocus
End Sub
Private Sub ComboBox7_Change()
'ComboBox8セット
Dim ico As Long
ico2 = 1
With ThisWorkbook.Worksheets("data")
ichi = Me.ComboBox6.Text
ichi1 = Me.ComboBox7.Text
Me.ComboBox8.Clear
Do While .Cells(ico2, 6) <> ""
If .Cells(ico2, 6) = ichi And .Cells(ico2, 7) = ichi1 Then 'もし6行目と7行めのアイテムだったら
ITE = .Cells(ico2, 8).Value
flg = 0
For I = 0 To Me.ComboBox8.ListCount - 1
If ITE = Me.ComboBox8.List(I) Then flg = 1
Next
If flg = 0 Then Me.ComboBox8.AddItem ITE
End If
ico2 = ico2 + 1
Loop
End With
Me.ComboBox8.SetFocus
End Sub