no experience for populate data in listbox based on selected item from another listbox

Amer Omar

New Member
Joined
Jan 27, 2024
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
Hi experts,
I search too much to populate data in listbox based on selected item from another listbox.
all of what I found it just by txtbox . I don't wanted .
I have two simple codes
first show data in listbox2 based on Expenses Detailes sheet as in image1
VBA Code:
Private Sub UserForm_Initialize()
   Dim LastRow As Long
   
   
    LastRow = Range("A" & Rows.Count).End(xlUp).Row    '<---- ??????
    With ListBox2
        .ColumnCount = 7
        .ColumnWidths = "70;70;170;130;70;70;70"
        .List = Range("A2:G" & LastRow).Value
    End With
    
End Sub

PP1.PNG




Form
ABCDEF
1DATEExchange authorization numberExpense NameExpense DescribtionAccount numberAmount
201/01/2024A00hospitalityLunch for three personssafe1300.00
301/01/2024A01StationeryFiles & Mony RubberMain Safe250.00
401/01/2024A02maintenanceTires & Discosafe11,200.00
502/01/2024A03maintenanceElectricity Carsafe12,500.00
602/01/2024A04hospitalityLunch for Four personsMain Safe430.00
702/01/2024A05StationeryPaper for PrinterMain Safe100.00
803/01/2024A06StationeryTransparent filessafe1120.00
903/01/2024A07Salaries of employees and workersTen Employees & workersMuth Bank20,000.00
1003/01/2024A08Goods packing workersThree Truckssafe1900.00
1103/01/2024A09hospitalityLunch for Six personssafe1750.00
1204/01/2024A10Office FurnitureThree Tables For OffieMuth Bank1,500.00
1304/01/2024A11Shop rentAdvanced monthMuth Bank7,600.00
1404/01/2024A12hospitalityLunch for Two personsMain Safe275.00
1504/01/2024A13maintenanceMechanicsafe11,670.00
Expenses Detailes


second image when select optionbutton1 based on Detailes sheet then will populate data in listbox1

VBA Code:
Private Sub OptionButton1_Click()
Dim LastRow As Long
   Dim ws As Worksheet
   Set ws = Sheets("Detailes")
   If OptionButton1.Value = True Then
   
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row    '<---- ??????
    With ListBox1
        .ColumnCount = 1
        .List = ws.Range("A2:A" & LastRow).Value
    End With
    End If
End Sub

PP2.PNG


Form
A
1EXPENSES NAME
2hospitality
3maintenance
4Personal withdrawals
5Cleaning materials
6Stationery
7Shop rent
8Salaries of employees and workers
9Renting the transportation of goods
10Workers unloading goods
11Goods packing workers
12Goods shipping costs
13Office Furniture
14Laptop & Office Furniture
15Office Furniture
16Cars
Detailes


what I expect as in picture.
should match selected item from listbox1 with the column(3) in listbox2 and filter data in listbox2 for the select item from listbox1 like this
PP3.PNG

I have no Experience for that ,I look forward from the expert to do that with considering data could be 10000 rows in Expenses Detailes.
Thanks.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
what I expect as in picture.
should match selected item from listbox1 with the column(3) in listbox2 and filter data in listbox2 for the select item from listbox1 like this

Try this (code in the userform module):
VBA Code:
Private Sub ListBox1_Click()

    Dim lastRow As Long, numRows As Long
    Dim r As Long, c As Long, n As Long
    Dim filteredData() As Variant
    
    Me.ListBox2.Clear
    
    With Worksheets("Expenses Detailes")
        
        Application.ScreenUpdating = False
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A1").CurrentRegion.AutoFilter Field:=3, Criteria1:=Me.ListBox1.List(Me.ListBox1.ListIndex)
        
        numRows = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
        If numRows > 0 Then
            ReDim filteredData(1 To numRows, 1 To 7)
            n = 0
            For r = 2 To lastRow
                If Not .Rows(r).EntireRow.Hidden Then
                    n = n + 1
                    For c = 1 To 7
                        filteredData(n, c) = .Cells(r, c).Value
                    Next
                End If
            Next
            Me.ListBox2.List = filteredData
        End If
        
        .Range("A1").CurrentRegion.AutoFilter
        Application.ScreenUpdating = True
                
    End With
        
End Sub
 
Upvote 0
Solution
Hi,
it's excellent ! :)
can you guide me how show date formating and numbrs like this
DD/MM/YYYY
#,##0.00
please?
 
Upvote 0
Two lines to add and I show where to add them:
VBA Code:
                    For c = 1 To 7
                        filteredData(n, c) = .Cells(r, c).Value
                    Next
                    filteredData(n, 1) = Format(filteredData(n, 1), "DD/MM/YYYY")
                    filteredData(n, 6) = Format(filteredData(n, 6), "#,##0.00")
 
Upvote 0
@John_w - hope don't mind making a suggestion - have you thought about using the Range.Text property when adding to the array?

Rich (BB code):
filteredData(n, c) = .Cells(r, c).Text

may resolve OPs issue

Dave
 
Upvote 0
@dmt32 - a good idea that hadn't occurred to me, thanks.

OP - the change is then simply this, and the 2 new lines aren't needed:

VBA Code:
                    For c = 1 To 7
                        filteredData(n, c) = .Cells(r, c).Text
                    Next
That will display values in the ListBox using the same formats as the sheet cells.
 
Upvote 0
Post the whole ListBox1_Click routine so I can see where you've put the 2 new lines.
this problem happened when I work at laptop in my work
here is the code
Rich (BB code):
Private Sub ListBox1_Click()

    Dim lastRow As Long, numRows As Long
    Dim r As Long, c As Long, n As Long
    Dim filteredData() As Variant
    
    Me.ListBox2.Clear
    
    With Worksheets("Expenses Detailes")
        
        Application.ScreenUpdating = False
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        .Range("A1").CurrentRegion.AutoFilter Field:=3, Criteria1:=Me.ListBox1.List(Me.ListBox1.ListIndex)
        
        numRows = .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
        If numRows > 0 Then
            ReDim filteredData(1 To numRows, 1 To 7)
            n = 0
            For r = 2 To lastRow
                If Not .Rows(r).EntireRow.Hidden Then
                    n = n + 1
                    For c = 1 To 7
                        filteredData(n, c) = .Cells(r, c).Value
                    Next
                    filteredData(n, 1) = Format(filteredData(n, 1), "DD/MM/YYYY")
                    filteredData(n, 6) = Format(filteredData(n, 6), "#,##0.00")
                End If
            Next
            Me.ListBox2.List = filteredData
        End If
        
        .Range("A1").CurrentRegion.AutoFilter
        Application.ScreenUpdating = True
                
    End With
        
End Sub
but now I'm at home works perfectly !(y)
i
I no know what's my bad!:rolleyes:
but I will check tomorrow and inform you what happens for me .;)
 
Upvote 0
Hi Dave !
based on your suggestion and without using suggestion in post # 4 that works perfectly .(y)
thanks buddy;)
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,109
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top