Hello to everybody
I found the following discussion very interesting and it is what I am looking for:
https://techcommunity.microsoft.com/t5/excel/excel-vba-userform-filter-listbox-content-by-dependent/m-p/3250102/thread-id/137352
However when I try to adapt to my scenario it doesn't work and I don't know where am I wrong (lack of vba!)
Combo 2 and 3 are not populated at all!
It is the same code except the sheet name and the column address for the combo:
- Column F, E, D original code
- Column B (combo1); I (combo2); Q (combo3) with an 18 listbox columns now.
Here the code:
Option Explicit
Private Sub ComboBox1_Change()
Dim sh As Worksheet
Dim r As Long
Dim m As Long
FillList
Me.ComboBox2.Clear
Set sh = Worksheets("New")
m = sh.Cells.Find(What:="\*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For r = 2 To m
If sh.Range("B" & r) = Me.ComboBox1.Value Then
If Application.WorksheetFunction.CountIf(sh.Range("I2:I" & r), sh.Range("I" & r)) = 1 Then
Me.ComboBox2.AddItem sh.Range("I" & r).Value
End If
End If
Next r
End Sub
Private Sub ComboBox2_Change()
Dim sh As Worksheet
Dim r As Long
Dim m As Long
FillList
Me.ComboBox3.Clear
Set sh = Worksheets("New")
m = sh.Cells.Find(What:="\*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For r = 2 To m
If sh.Range("I" & r) = Me.ComboBox2.Value Then
If Application.WorksheetFunction.CountIf(sh.Range("Q2:Q" & r), sh.Range("Q" & r)) = 1 Then
Me.ComboBox3.AddItem sh.Range("Q" & r).Value
End If
End If
Next r
End Sub
Private Sub ComboBox3_Change()
FillList
End Sub
Private Sub UserForm_Initialize()
Dim sh As Worksheet
Dim r As Long
Dim m As Long
Set sh = Sheets("New")
m = sh.Cells.Find(What:="\*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For r = 2 To m
If Application.WorksheetFunction.CountIf(sh.Range("B2:B" & r), sh.Range("B" & r)) = 1 Then
Me.ComboBox1.AddItem sh.Range("B" & r).Value
End If
Next r
FillList
End Sub
Private Sub FillList()
Dim sh As Worksheet
Dim r As Long
Dim c As Long
Dim m As Long
Dim arr()
Dim n As Long
Dim f As Boolean
Set sh = Worksheets("New")
m = sh.Cells.Find(What:="\*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For r = 2 To m
f = (sh.Range("B" & r).Value = Me.ComboBox1.Value) Or (Me.ComboBox1.ListIndex = -1)
If f Then
f = (sh.Range("I" & r).Value = Me.ComboBox2.Value) Or (Me.ComboBox2.ListIndex = -1)
If f Then
f = (sh.Range("Q" & r).Value = Me.ComboBox3.Value) Or (Me.ComboBox3.ListIndex = -1)
End If
End If
If f Then
n = n + 1
ReDim Preserve arr(1 To 18, 1 To n)
' Store the row number in the first (hidden) column
arr(1, n) = r
For c = 2 To 17
arr(c + 1, n) = sh.Cells(r, c).Value
Next c
End If
Next r
If n > 0 Then
Me.ListBox1.Column = arr
Else
Me.ListBox1.Clear
End If
End Sub
Can anyone please help? Thank a lot in advance.
JohnBi