select range from combobox to populate data in listbox

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hi
this code for DanteAmore and I modified to fit with my requirements but I faild
my goal select the header from combobox1 by matching with header in column G then should populate data in the listbox for the range is relating with the header in columns (F:I) inside the sheet. .
VBA Code:
Option Explicit
Dim a As Variant

Sub FilterData()
  Dim txt1 As String
  Dim I As Long, j As Long, k As Long
  
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For I = 1 To UBound(a)
    If ComboBox1.Value = "" Then txt1 = a(I, 2) Else txt1 = ComboBox1.Value
    
    If LCase(a(I, 2)) Like LCase(txt1) & "*" Then
      k = k + 1
      For j = 1 To 4
        b(k, j) = a(I, j)
      Next
    End If
  Next
  If k > 0 Then ListBox1.List = b
End Sub

Private Sub ComboBox1_Change()
 Call FilterData
End Sub

Private Sub UserForm_Activate()
  a = Sheets("RANGES").Range("F2:I" & Sheets("RANGES").Range("G" & Rows.Count).End(3).Row).Value
End Sub
data in sheet
RANGES.xlsm
ABCDEFGHI
1SRSR
2ITEMDATETYPETOTALITEMDATENOT PAIDPAID
3110/06/2023NOT PAID7,720.00110/06/20239,720.0010,810.00
4210/06/2023NOT PAID2,000.00211/06/20237,000.000.00
5310/06/2023PAID6,810.00SUM16,720.0010,810.00
6410/06/2023PAID4,000.00
7511/06/2023NOT PAID7,000.00SVR
8SUM27,530.00ITEMDATENOT PAIDPAID
9110/06/20235,720.0014,040.00
10SVR211/06/202313,230.000.00
11ITEMDATETYPETOTAL312/06/20230.0015,000.00
12110/06/2023NOT PAID5,720.00SUM18,950.0029,040.00
13210/06/2023PAID14,040.00
14311/06/2023NOT PAID1,230.00SDE
15411/06/2023NOT PAID12,000.00ITEMDATENOT PAIDPAID
16512/06/2023PAID14,000.00110/06/20230.005,000.00
17612/06/2023PAID1,000.00211/06/20232,200.002,000.00
18SUM47,990.00312/06/20233,000.000.00
19SUM5,200.007,000.00
20
21SDEFGR
22ITEMDATETYPETOTALITEMDATENOT PAIDPAID
23110/06/2023PAID4,000.00110/06/20230.002,500.00
24210/06/2023PAID1,000.00211/06/20231,100.000.00
25311/06/2023PAID2,000.00312/06/20239,200.000.00
26411/06/2023NOT PAID2,200.00SUM10,300.002,500.00
27512/06/2023NOT PAID3,000.00
28SUM12,200.00
29
30
31FGR
32ITEMDATETYPETOTAL
33110/06/2023PAID1,200.00
34210/06/2023PAID1,300.00
35311/06/2023NOT PAID1,100.00
36412/06/2023NOT PAID2,200.00
37512/06/2023NOT PAID7,000.00
38SUM12,800.00
39
RANGES
Cell Formulas
RangeFormula
D8,D38,D28D8=SUM(D3:D7)
D18D18=SUM(D12:D17)


here example when select the header in combobox1
IMAGE.PNG
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Abdo:

Check if this is what you want:
1721872076826.png


Replace all your code for this:

VBA Code:
Option Explicit

Sub FilterData()
  Dim sh As Worksheet
  Dim f As Range, ar As Range
  Dim i As Long, j As Long, n As Long
  Dim col As Variant
  
  Set sh = Sheets("RANGES")
  ListBox1.Clear
  For Each col In Array("B:B", "G:G")
    Set f = sh.Range(col).Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      For i = f.Row + 1 To sh.Range("A" & Rows.Count).End(3).Row
      
        With ListBox1
          .AddItem
          .List(n, 0) = sh.Cells(i, f.Column - 1 + 0).Value
          .List(n, 1) = Format(sh.Cells(i, f.Column - 1 + 1).Value, "dd/mm/yyyy")
          .List(n, 2) = sh.Cells(i, f.Column - 1 + 2).Value
          .List(n, 3) = Format(sh.Cells(i, f.Column - 1 + 3).Value, "#,##0.00")
        End With
        
        n = n + 1
        If sh.Cells(i, f.Column - 1).Value = "SUM" Then
          Exit For
        End If
      Next
    End If
  Next
End Sub

Private Sub ComboBox1_Change()
 Call FilterData
End Sub

Note: You should have previously loaded the combobox.


I hope to hear from you
Dante Amor
 
Upvote 0
perfect !
just I would for column G:G , I don't need column B:B
I try to change this
VBA Code:
  For Each col In Array("B:B", "G:G")
to
VBA Code:
  For Each col In range("G:G")

and from
VBA Code:
 Dim col As Variant
to
VBA Code:
 Dim col As Range
but shows error Method range of object worksheet failed in this line
VBA Code:
Set f = sh.Range(col).Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
may you help me ,please?
 
Upvote 0
Try:

VBA Code:
Sub FilterData()
  Dim sh As Worksheet
  Dim f As Range, ar As Range
  Dim i As Long, j As Long, n As Long
  Dim col As Variant
  
  Set sh = Sheets("RANGES")
  ListBox1.Clear
    Set f = sh.Range("B:B").Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      For i = f.Row + 1 To sh.Range("A" & Rows.Count).End(3).Row
      
        With ListBox1
          .AddItem
          .List(n, 0) = sh.Cells(i, f.Column - 1 + 0).Value
          .List(n, 1) = Format(sh.Cells(i, f.Column - 1 + 1).Value, "dd/mm/yyyy")
          .List(n, 2) = sh.Cells(i, f.Column - 1 + 2).Value
          .List(n, 3) = Format(sh.Cells(i, f.Column - 1 + 3).Value, "#,##0.00")
        End With
        
        n = n + 1
        If sh.Cells(i, f.Column - 1).Value = "SUM" Then
          Exit For
        End If
      Next
    End If
End Sub

Private Sub ComboBox1_Change()
 Call FilterData
End Sub

😇
 
Upvote 0
Solution
just I would for column G:G , I don't need column B:B
I changed to
VBA Code:
    Set f = sh.Range("G:G").Find(ComboBox1.Value, , xlValues, xlWhole, , , False)
now works greatly , thank you so much .:):)
 
Upvote 1

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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