brendalpzm
Board Regular
- Joined
- Oct 3, 2022
- Messages
- 59
- Office Version
- 365
- 2021
- 2019
- 2016
- Platform
- Windows
I have a ListBox that is filtered by a TextBox, but I want it ot be filter by a ComboBox as well, This means that If the TextBox is filtering specific values, the values shown in the ListBox must be shown in the ComboBox.
This is a visual example of it
And this is the code that I'm using for the filter if it's useful for reference
VBA Code:
Option Explicit
Dim arrdata() As Variant 'at the beginning of all the code
Private Sub ModelCB_Change()
Call Filter_Data
End Sub
Private Sub UserFilter_Change()
Call Filter_Data
End Sub
Sub Filter_Data()
Dim i As Long, lngrow As Long
Dim tbox As String, cbox As String
Me.CarList.Clear
lngrow = 0
For i = LBound(arrdata, 1) + 1 To UBound(arrdata, 1)
If UserFilter.Value = "" Then tbox = LCase(arrdata(i, 5)) Else tbox = LCase(UserFilter.Value)
If ModelCB.Value = "" Then cbox = LCase(arrdata(i, 3)) Else cbox = LCase(ModelCB.Value)
If arrdata(i, 13) <> "completed" And LCase(arrdata(i, 5)) Like "*" & tbox & "*" And _
LCase(arrdata(i, 3)) = cbox Then
Call add_ToListbox(lngrow, i)
lngrow = lngrow + 1
End If
Next
End Sub
Private Sub UserForm_Initialize()
Dim lngindex As Long
Dim lngrow As Long
arrdata = Worksheets("Current").Range("A1").CurrentRegion.Value
With Me.lstHeaders
.ColumnCount = 7
.ColumnWidths = "180;60;255;95;75;105;65"
.Font.Size = 13
.Font.Bold = True
.Enabled = False
.AddItem
.List(lngrow, 0) = arrdata(1, 3)
.List(lngrow, 1) = arrdata(1, 4)
.List(lngrow, 2) = arrdata(1, 5)
.List(lngrow, 3) = arrdata(1, 10)
.List(lngrow, 4) = arrdata(1, 11)
.List(lngrow, 5) = arrdata(1, 13)
.List(lngrow, 6) = arrdata(1, 14)
End With
With Me.CarList
.ColumnCount = 7
.ColumnWidths = "180;60;255;95;75;105;65"
.Font.Size = 13
End With
lngrow = 0
For lngindex = LBound(arrdata, 1) + 1 To UBound(arrdata, 1)
If arrdata(lngindex, 13) <> "completed" Then
Call add_ToListbox(lngrow, lngindex)
lngrow = lngrow + 1
End If
Next lngindex
End Sub
Sub add_ToListbox(lngrow, lngindex)
With Me.CarList
.AddItem
.List(lngrow, 0) = arrdata(lngindex, 3)
.List(lngrow, 1) = arrdata(lngindex, 4)
.List(lngrow, 2) = arrdata(lngindex, 5)
.List(lngrow, 3) = arrdata(lngindex, 10)
.List(lngrow, 4) = arrdata(lngindex, 11)
.List(lngrow, 5) = arrdata(lngindex, 13)
.List(lngrow, 6) = arrdata(lngindex, 14)
End With
End Sub
[/CODE]
This is a visual example of it
And this is the code that I'm using for the filter if it's useful for reference
VBA Code:
VBA Code:
Dim arrdata() As Variant 'at the beginning of all the code
Private Sub ModelCB_Change()
Call Filter_Data
End Sub
Private Sub UserFilter_Change()
Call Filter_Data
End Sub
Sub Filter_Data()
Dim i As Long, lngrow As Long
Dim tbox As String, cbox As String
Me.CarList.Clear
lngrow = 0
For i = LBound(arrdata, 1) + 1 To UBound(arrdata, 1)
If UserFilter.Value = "" Then tbox = LCase(arrdata(i, 5)) Else tbox = LCase(UserFilter.Value)
If ModelCB.Value = "" Then cbox = LCase(arrdata(i, 3)) Else cbox = LCase(ModelCB.Value)
If arrdata(i, 13) <> "completed" And LCase(arrdata(i, 5)) Like "*" & tbox & "*" And _
LCase(arrdata(i, 3)) = cbox Then
Call add_ToListbox(lngrow, i)
lngrow = lngrow + 1
End If
Next
End Sub
Private Sub UserForm_Initialize()
Dim lngindex As Long
Dim lngrow As Long
arrdata = Worksheets("Current").Range("A1").CurrentRegion.Value
With Me.lstHeaders
.ColumnCount = 7
.ColumnWidths = "180;60;255;95;75;105;65"
.Font.Size = 13
.Font.Bold = True
.Enabled = False
.AddItem
.List(lngrow, 0) = arrdata(1, 3)
.List(lngrow, 1) = arrdata(1, 4)
.List(lngrow, 2) = arrdata(1, 5)
.List(lngrow, 3) = arrdata(1, 10)
.List(lngrow, 4) = arrdata(1, 11)
.List(lngrow, 5) = arrdata(1, 13)
.List(lngrow, 6) = arrdata(1, 14)
End With
With Me.CarList
.ColumnCount = 7
.ColumnWidths = "180;60;255;95;75;105;65"
.Font.Size = 13
End With
lngrow = 0
For lngindex = LBound(arrdata, 1) + 1 To UBound(arrdata, 1)
If arrdata(lngindex, 13) <> "completed" Then
Call add_ToListbox(lngrow, lngindex)
lngrow = lngrow + 1
End If
Next lngindex
End Sub
Sub add_ToListbox(lngrow, lngindex)
With Me.CarList
.AddItem
.List(lngrow, 0) = arrdata(lngindex, 3)
.List(lngrow, 1) = arrdata(lngindex, 4)
.List(lngrow, 2) = arrdata(lngindex, 5)
.List(lngrow, 3) = arrdata(lngindex, 10)
.List(lngrow, 4) = arrdata(lngindex, 11)
.List(lngrow, 5) = arrdata(lngindex, 13)
.List(lngrow, 6) = arrdata(lngindex, 14)
End With
End Sub
[/CODE]