Selected Answer
Hi again Tubrak
In the attached revised workbook, I changed some things in the code for UserForm1 so the other sheets can be used.
Firstly I hid ComboBoxes 1 and 1 until a sheet has been selected from ComboBox3 (changes in bold):
Private Sub UserForm_Activate()
Dim ws As Worksheet
For Each ws In Worksheets
ComboBox3.AddItem ws.Name
Next ws
' disable other inputs until a sheet is picked
ComboBox1.Visible = False
ComboBox2.Visible = False
End Sub
Private Sub UserForm_Initialize()
'ComboBox1.List = Filter([if(isnumber(4:4),text(4:4,"yyyy/m/d"))], False, 0)
'ComboBox2.List = Array("Zero", "Not Zero")
End Sub
When the user picks a sheet from ComboBox3, a new procedure for ComboBox3_Change checks it's still a valid sheet then populates CB1 and CB2 to suit and makes them visible:
Private Sub ComboBox3_Change()
Dim ws As Worksheet, wsMatch As Boolean
' check it matches a sheet (and isn't been changed or cleared)
For Each ws In Worksheets
If ComboBox3.Text = ws.Name Then wsMatch = True
Next ws
' quit if not found
If wsMatch <> True Then
ComboBox1.Visible = False
ComboBox2.Visible = False
MsgBox "Pick a date from the dropdown list instead"
Exit Sub
End If
' now populate other ComboBoxes...
ComboBox2.List = Array("Zero", "Not Zero")
'... to suit sheet selected
With Worksheets(ComboBox3.Text)
.Select
ComboBox1.List = Filter([if(isnumber(4:4),text(4:4,"yyyy/m/d"))], False, 0)
End With
' reveal other ComboBoxes
ComboBox1.Visible = True
ComboBox2.Visible = True
End Sub
Finally, when CB1 (or CB2) changes, the sub FillListBox is triggered with the following changes (in bold):
Sub FillListBox()
Dim a, b, x, d As Date, col As Variant, s, i As Long
Me.ListBox1.Clear
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
x = Split(Me.ComboBox1, "/")
d = DateSerial(x(0), x(1), x(2))
s = Choose(Me.ComboBox2.ListIndex + 2, "", "=0", "<>0")
'With Sheets("stock").[a4].CurrentRegion
' use sheet from ComboBox3
With Worksheets(ComboBox3.Text).[a4].CurrentRegion
' used .row(1) of region, not 5 of sheet
col = Application.Match(CLng(d), .Rows(1), 0)
If IsError(col) Then MsgBox "Can not get column matches to " & Me.ComboBox1: Unload Me: Exit Sub
a = .Value: x = Evaluate("transpose(row(5:" & .Rows.count & "))")
If s <> "" Then
x = Filter(.Parent.Evaluate("transpose(if((row(" & .Columns(col).Address & _
")>1)*(" & .Columns(col).Address & s & "),row(" & .Address & ")))"), False, 0)
If UBound(x) = -1 Then Exit Sub
ReDim Preserve x(1 To UBound(x) + 1)
End If
ReDim b(1 To UBound(x))
For i = 1 To UBound(x)
b(i) = Array(i, a(x(i), 2), Format(a(x(i), col), "#,0.00"))
Next
End With
With Me.ListBox1
If UBound(b) = 1 Then
.Column = b(1)
Else
.List = Application.Index(b, 0, 0)
End If
.ColumnCount = 3
.ColumnWidths = "40;250;60"
End With
End Sub
That should give a ListBox showing data from the chosen date column.
Hope this is what you wanted. If so, please mark this Answer as Selected.