Excel VBA Userform: Filtering a Listbox and Showing a "Live" Version of the Filtered Data Based on a Texbox Value

bauer32

New Member
Joined
Jul 23, 2019
Messages
2
I have a "Data" worksheet that I want users to be able to edit the data in this worksheet by using a Form (keep the data uniform). When the user opens the initial form (UserForm1), this will display all of the rows of data that they can select from to edit. Because this data set is large (will be 6,000 rows of data), I have included textboxes in the form so they can filter down to the data they want to edit. The textboxes filter the "Data" worksheet, but my listbox doesn't show the "live" filtered version as the user uses the textboxes. Below is the code I am using to add the data to the Listbox and the GetRange function (I will attach a copy of the spreadsheet also). I know excel has the .SpecialCells(xlCellTypeVisible), but I cannot figure out how to use this to show the filtered data. I'm using the .RowSource = rg.Address(external:=True), to add the data to the listbox.

I have seen people use the .AddItem and loop through the data using the .List creating an array list, but that seems to be slow, cannot add more than 10 columns, and I cannot use multiple textboxes to filter at the same time unless the user types and then selects a button which uses all of the textbox data.

Private Sub UserForm_Activate()
Call AddDataToListBox
End Sub

Private Sub AddDataToListBox()

Dim rg As Range
Set rg = GetRange()

'Link the data to the ListBox

With ListBox_SelectDataToEdit

.RowSource = rg.Address(external:=True)
.ColumnCount = 12
.ColumnWidths = "55;210;210;0;120;55;40;45;45;60;55;40"
.ColumnHeads = True
.ListIndex = 0

End With

End Sub

Function GetRange() As Range

Set sh = ThisWorkbook.Sheets("Data")

Set GetRange = sh.Range("B5:N3000")
Set GetRange = GetRange.Offset(1, 0).Resize(GetRange.Rows.Count - 1)

End Function

Please let me know if I was unclear on anything or if you need a better explanation.

Thank you!!
 

Attachments

  • Data Worksheet and Form.png
    Data Worksheet and Form.png
    168.3 KB · Views: 193
  • Filtering Data Worksheet But Listbox Doesn't Update With Filtered Data.png
    Filtering Data Worksheet But Listbox Doesn't Update With Filtered Data.png
    202.6 KB · Views: 198

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I have a "Data" worksheet that I want users to be able to edit the data in this worksheet by using a Form (keep the data uniform). When the user opens the initial form (UserForm1), this will display all of the rows of data that they can select from to edit. Because this data set is large (will be 6,000 rows of data), I have included textboxes in the form so they can filter down to the data they want to edit. The textboxes filter the "Data" worksheet, but my listbox doesn't show the "live" filtered version as the user uses the textboxes. Below is the code I am using to add the data to the Listbox and the GetRange function (I will attach a copy of the spreadsheet also). I know excel has the .SpecialCells(xlCellTypeVisible), but I cannot figure out how to use this to show the filtered data. I'm using the .RowSource = rg.Address(external:=True), to add the data to the listbox.

I have seen people use the .AddItem and loop through the data using the .List creating an array list, but that seems to be slow, cannot add more than 10 columns, and I cannot use multiple textboxes to filter at the same time unless the user types and then selects a button which uses all of the textbox data.

Private Sub UserForm_Activate()
Call AddDataToListBox
End Sub

Private Sub AddDataToListBox()

Dim rg As Range
Set rg = GetRange()

'Link the data to the ListBox

With ListBox_SelectDataToEdit

.RowSource = rg.Address(external:=True)
.ColumnCount = 12
.ColumnWidths = "55;210;210;0;120;55;40;45;45;60;55;40"
.ColumnHeads = True
.ListIndex = 0

End With

End Sub

Function GetRange() As Range

Set sh = ThisWorkbook.Sheets("Data")

Set GetRange = sh.Range("B5:N3000")
Set GetRange = GetRange.Offset(1, 0).Resize(GetRange.Rows.Count - 1)

End Function

Please let me know if I was unclear on anything or if you need a better explanation.

Thank you!!
*********Sorry, realize I forgot to use the VBA button when posting my code.*********

I have a "Data" worksheet that I want users to be able to edit the data in this worksheet by using a Form (keep the data uniform). When the user opens the initial form (UserForm1), this will display all of the rows of data that they can select from to edit. Because this data set is large (will be 6,000 rows of data), I have included textboxes in the form so they can filter down to the data they want to edit. The textboxes filter the "Data" worksheet, but my listbox doesn't show the "live" filtered version as the user uses the textboxes. Below is the code I am using to add the data to the Listbox and the GetRange function (I will attach a copy of the spreadsheet also). I know excel has the .SpecialCells(xlCellTypeVisible), but I cannot figure out how to use this to show the filtered data. I'm using the .RowSource = rg.Address(external:=True), to add the data to the listbox.

I have seen people use the .AddItem and loop through the data using the .List creating an array list, but that seems to be slow, cannot add more than 10 columns, and I cannot use multiple textboxes to filter at the same time unless the user types and then selects a button which uses all of the textbox data. I have also tried the copy to a "template" worksheet to create a new list with the filters, but that doesn't seem to work well when the users are constantly changing the data in the textbox filters.

VBA Code:
Private Sub UserForm_Activate()
    Call AddDataToListBox
End Sub

Private Sub AddDataToListBox()

Dim rg As Range
Set rg = GetRange()

'Link the data to the ListBox

    With ListBox_SelectDataToEdit

        .RowSource = rg.Address(external:=True)
        .ColumnCount = 12
        .ColumnWidths = "55;210;210;0;120;55;40;45;45;60;55;40"
        .ColumnHeads = True
        .ListIndex = 0
 
    End With
 
End Sub

Function GetRange() As Range

Set sh = ThisWorkbook.Sheets("Data")

Set GetRange = sh.Range("B5:N3000")
Set GetRange = GetRange.Offset(1, 0).Resize(GetRange.Rows.Count - 1)

End Function

Please let me know if I was unclear on anything or if you need a better explanation.

Thank you!!
 

Attachments

  • Data Worksheet and Form.png
    Data Worksheet and Form.png
    168.3 KB · Views: 104
  • Filtering Data Worksheet But Listbox Doesn't Update With Filtered Data.png
    Filtering Data Worksheet But Listbox Doesn't Update With Filtered Data.png
    202.6 KB · Views: 105
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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