Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

populate data in listbox based on selected sheet

0

Hello,

I try adapting code when select sheet from combobox3  and select date from combobox1 should populate data for selected sheet and when select  sheet from combobox3 and select date from combobox1 and select case from combobox2 then should populate data for selected sheet.

any help for this adapting,please?

Answer
Discuss

Answers

0

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. 

Discuss

Discussion

thanks John,
but I'm not sure why you disable  CB2   from work, I need it.
and even I enabled  CB2 the code doesn't work anymore when select ZERO  or NOT  ZEROR after select sheet and date will shows error !
I don't know what my bad.
tubrak (rep: 26) Sep 16, '26 at 5:03 am
Tubrak - I just disabled CB2 only until a sheet is selected .

I didn't try Zero and Non-zero filters but will try tomorrow .
John_Ru (rep: 6842) Sep 16, '26 at 3:12 pm
Tubrak

I took a look at this and realised that although my version swaps sheets, it skips the first 4 rows and doesn't work if a filter is needed. I spend an hour or more trying to work out where is goes wrong but couldn't. Sorry but I can't spare any more time trying to fix this problem.
John_Ru (rep: 6842) Sep 17, '26 at 9:41 am
anyway thanks for your efforts.
tubrak (rep: 26) Sep 17, '26 at 10:13 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login