doesn't show data in listbox on form without any error in this code

Abdo

Board Regular
Joined
May 16, 2022
Messages
201
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I try to populate data in listbox on form based on selected items from combobox1,combobox2
combobox1 will match with column A and combobox2 will match with column E then should populate data in list box
here is code getting from this forum and I modified based on my requirements.
VBA Code:
Private Sub CommandButton1_Click()
   
   Dim i As Long, j As Long, k As Long
    Dim cb1 As String
    Dim cb2 As String
    
    a = Sheets("Sheet3").Range("A1:F" & Sheets("Sheet3").Range("F" & Rows.Count).End(3).Row).Value
    
     With ListBox2
     .ColumnWidths = "40;50;50;40;60;40"
     .columnCount = 6
     .Font.Size = 10
     
        End With
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For i = 1 To UBound(a)
  If ComboBox1.Value = "" Then cb1 = a(i, 1) Else cb1 = ComboBox1.Value
    If ComboBox2.Value = "" Then cb2 = a(i, 5) Else cb1 = ComboBox2.Value
    If LCase(a(i, 1)) Like LCase(cb1) & "*" And LCase(a(i, 5)) Like LCase(cb2) & "*" Then
  k = k + 1
      For j = 1 To 6
        b(k, j) = a(i, j)
      Next
    End If
    Next
      If k > 0 Then ListBox2.List = b

    If ListBox2.ListCount = 0 Then
        MsgBox "doesn't show data"
    End If
   
End Sub
I hope some body help me.
thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi Abdo:

Check this:
- The commandbutton is not necesary.​
- Now every time you modify any combobox the listbox will automatically be filled.​
- Must be cb2:​
If ComboBox2.Value = "" Then cb2 = a(i, 5) Else cb1 cb2 = ComboBox2.Value​
- I assume you already loaded the values into each combobox.​

Replace your code for this:

VBA Code:
Dim a As Variant                'At the beginning of all the code

Private Sub ComboBox1_Change()
  Call FilterData
End Sub
Private Sub ComboBox2_Change()
  Call FilterData
End Sub

Private Sub FilterData()
  Dim i As Long, j As Long, k As Long
  Dim cb1 As String, cb2 As String
   
  ListBox2.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a)
    If ComboBox1.Value = "" Then cb1 = a(i, 1) Else cb1 = ComboBox1.Value
    If ComboBox2.Value = "" Then cb2 = a(i, 5) Else cb2 = ComboBox2.Value
    If LCase(a(i, 1)) Like LCase(cb1) & "*" And LCase(a(i, 5)) Like LCase(cb2) & "*" Then
      k = k + 1
      For j = 1 To 6
        b(k, j) = a(i, j)
      Next
    End If
  Next
 
  If k = 0 Then
    MsgBox "doesn't show data"
  Else
    ListBox2.List = b
  End If
End Sub

Private Sub UserForm_Activate()
  a = Sheets("Sheet3").Range("A1:F" & Sheets("Sheet3").Range("F" & Rows.Count).End(3).Row).Value
  With ListBox2
    .RowSource = ""
    .ColumnWidths = "40;50;50;40;60;40"
    .ColumnCount = 6
    .Font.Size = 10
  End With

  Call FilterData
End Sub

🤗
 
Upvote 0
still doesn't show data by show message "doesn't show data" !
It is not enough to put it does not work, you must put what data you have on the sheet, what you are putting in each combobox.

Check these 4 points:
  1. Did you copy the entire code?
  2. Remember that the declaration of the variable 'a' must go at the beginning of all the code.
  3. Did you make changes to my code?
  4. How are you loading the comboboxes and what data does each combobox have?

In my tests the filters work wonderfully. See the example below.

Without filter:
1729781779097.png

With filter combobox1:
1729781826141.png

With filter combo1 and combo2:
1729781868544.png

;)
 
Upvote 0
  1. Did you copy the entire code?
surely yes.
  1. Remember that the declaration of the variable 'a' must go at the beginning of all the code.
I made sure about it .
  1. Did you make changes to my code?
surely I didn't
  • How are you loading the comboboxes and what data does each combobox have?
I did by manually link cells with row source in properties form .I have not code to do automatically.
here is data


da.xlsm
ABCDEF
1CODEBRANDPRICEQTYWHAREHOUSEDATE
2100Produit50149magasin101/05/2024
3100Produit5017magasin101/01/2024
4100Produit5025magasin201/01/2024
5100Produit5025magasin301/01/2024
6101Produit1700magasin01/01/2024
7101Produit17013magasin101/01/2024
8101Produit17051magasin201/01/2024
9102Produit28584magasin301/01/2024
10102Produit28539magasin01/01/2024
11102Produit2859magasin101/01/2024
12103Produit39050magasin201/01/2024
13103Produit39083magasin301/01/2024
14104Produit49065magasin301/01/2024
15101Produit1706magasin01/05/2024
16102Produit28511magasin01/05/2024
17103Produit39050magasin301/05/2024
18100Produit5012magasin01/05/2024
Sheet3

here is used code
VBA Code:
Dim a As Variant                'At the beginning of all the code

Private Sub ComboBox1_Change()
  Call FilterData
End Sub
Private Sub ComboBox2_Change()
  Call FilterData
End Sub

Private Sub FilterData()
  Dim i As Long, j As Long, k As Long
  Dim cb1 As String, cb2 As String
   
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a)
    If ComboBox1.Value = "" Then cb1 = a(i, 1) Else cb1 = ComboBox1.Value
    If ComboBox2.Value = "" Then cb2 = a(i, 5) Else cb2 = ComboBox2.Value
    If LCase(a(i, 1)) Like LCase(cb1) & "*" And LCase(a(i, 5)) Like LCase(cb2) & "*" Then
      k = k + 1
      For j = 1 To 6
        b(k, j) = a(i, j)
      Next
    End If
  Next
 
  If k = 0 Then
    MsgBox "doesn't show data"
  Else
    ListBox1.List = b
  End If
End Sub

Private Sub UserForm_Activate()
  a = Sheets("Sheet3").Range("A1:F" & Sheets("Sheet3").Range("F" & Rows.Count).End(3).Row).Value
  With ListBox1
    .RowSource = ""
    .ColumnWidths = "40;50;50;40;60;40"
    .ColumnCount = 6
    .Font.Size = 10
  End With

  Call FilterData
End Sub
 
Upvote 0
I did by manually link cells with row source in properties form .I have not code to do automatically.
here is data
The problem is the data you are putting in the combobox.
Delete what you did. I do it with the code.



combobox1 will match with column A and combobox2 will match with column E then should populate data in list box
If I load the combos it works wonderfully:

1729797767274.png

1729797938423.png


Use the following code, I added some lines to load the comboboxes.

VBA Code:
Dim a As Variant                'At the beginning of all the code

Private Sub ComboBox1_Change()
  Call FilterData
End Sub
Private Sub ComboBox2_Change()
  Call FilterData
End Sub

Private Sub FilterData()
  Dim i As Long, j As Long, k As Long
  Dim cb1 As String, cb2 As String
    
  ListBox2.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For i = 1 To UBound(a)
    If ComboBox1.Value = "" Then cb1 = a(i, 1) Else cb1 = ComboBox1.Value
    If ComboBox2.Value = "" Then cb2 = a(i, 5) Else cb2 = ComboBox2.Value
    If LCase(a(i, 1)) Like LCase(cb1) & "*" And LCase(a(i, 5)) Like LCase(cb2) & "*" Then
      k = k + 1
      For j = 1 To 6
        b(k, j) = a(i, j)
      Next
    End If
  Next
  
  If k = 0 Then
    MsgBox "doesn't show data"
  Else
    ListBox2.List = b
  End If
End Sub

Private Sub UserForm_Activate()
  Dim dic1 As Object, dic2 As Object
  Dim i As Long
  
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  a = Sheets("Sheet3").Range("A1:F" & Sheets("Sheet3").Range("F" & Rows.Count).End(3).Row).Value
  With ListBox2
    .RowSource = ""
    .ColumnWidths = "40;50;50;40;150;40"
    .ColumnCount = 6
    .Font.Size = 10
  End With

  For i = 2 To UBound(a, 1)
    dic1(a(i, 1)) = Empty
    dic2(a(i, 5)) = Empty
  Next
  
  ComboBox1.List = dic1.keys
  ComboBox2.List = dic2.keys

  Call FilterData
End Sub

🧙‍♂️
 
Upvote 0
Solution

Forum statistics

Threads
1,222,903
Messages
6,168,939
Members
452,227
Latest member
sam1121

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