Excel VBA - Trouble Filtering Worksheet Based on ListBox Selections

sax2play

Board Regular
Joined
Mar 13, 2021
Messages
63
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am trying to filter a worksheeet based on the selections a user makes in a listbox; however, when the sheet filters, all of the data is blank. The listbox is populated dynamically with a set of timestamps from the column that will be filtered on based on selection. Looking for any help in why my code is not working.

VBA Code:
Private Sub CommandButton1_Click()

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    
    'Set workbook and worksheet variables
    Dim WB As Workbook: Set WB = ActiveWorkbook
    Dim shCompare As Worksheet
    Set shCompare = WB.Worksheets("Fault Comparison")
    
    'Filter MERS Report
    Dim rngFound As Range
    Dim Q As Long, n As Long
    
    With Me.ListBox1
        Dim arr() As Variant
        Dim Q As Long, n As Long
        For Q = 0 To .ListCount - 1
            If .Selected(Q) Then
                ReDim Preserve arr(0 To n)
                arr(n) = .List(Q)
                n = n + 1
            End If
        Next Q
    End With
    
    Set rngFound = WB.Sheets("Fault Comparison").Range("5:5").Find(What:="Test Timestamp", LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=True)
    
    shCompare.Select
    shCompare.Cells(5, rngFound.Column).Select
    rngFound.AutoFilter Field:=rngFound.Column, Criteria1:=arr, Operator:=xlFilterValues

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    

End Sub

Thanks in advance for any assistance with this!
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
How is your timestamp on the sheet?
How is your timestamp in the listbox?
Could you put an image to see how the data is?


This way works for me.

1668403484422.png



VBA Code:
Private Sub CommandButton1_Click()
  Dim WB As Workbook
  Dim shCompare As Worksheet
  Dim arr() As Variant
  Dim Q As Long, n As Long
  Dim rngFound As Range
  
  Set WB = ActiveWorkbook
  Set shCompare = WB.Worksheets("Fault Comparison")

  'Filter MERS Report
  With Me.ListBox1
    For Q = 0 To .ListCount - 1
      If .Selected(Q) Then
        ReDim Preserve arr(0 To n)
        arr(n) = .List(Q)
        n = n + 1
      End If
    Next Q
  End With

  shCompare.Select
  Set rngFound = shCompare.Range("5:5").Find("Test Timestamp", , xlValues, xlWhole, , , True)
  If Not rngFound Is Nothing Then
    rngFound.AutoFilter Field:=rngFound.Column, Criteria1:=arr, Operator:=xlFilterValues
  End If
End Sub
 
Upvote 0
This is before I attempt to filter:
1668433523653.png



This is after attempting to filter:
1668433606499.png


It does not matter if I select one or multiple - it always filters the sheet to blank.

Thanks!
 
Upvote 0
I'm not sure if this matters or not, but when running through the code, the date selected in the listbox is converted to number format for .List(Q) variable (see picture below, though hard to see). If I change the column being filtered to number format, it still filters the sheet to blank.

1668434590287.png


Thanks!
 
Upvote 0
works for me.

1668442568318.png


How are you loading the data into the listbox?
Could you share all your code?

And maybe a copy of your file to review.
You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Links to files below. Open file 11_2_2022 and run the SV_Compare_New Macro from VBA for the code. The macro will prompt you to select the other file (SV file), which will pull the data into the sheet. That data is then used to populate the listbox. I currently have "Stop" in place for when it gets to the section for filtering.

I only have code so far for filtering from the timestamp listbox, not the other two.

Thanks in advance for all your help and I apologize for the code - I am not a coder at all so it is very chaotic/unorganized.

11_2_2022 file:

SV File:
 
Upvote 0
works for me.

View attachment 78640

How are you loading the data into the listbox?
Could you share all your code?

And maybe a copy of your file to review.
You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
Dante - were you able to take a look by chance and see why I cannot get my sheet to filter properly?

Thanks for all your help!
 
Upvote 0
I did a test and it didn't work because in your userform code you have this line:
Set Filter_Sheets = WB.Worksheets("ConfigFilter_Sheets")


And that sheet does not exist in any of the files.

In the button you have this sheet, it doesn't exist in the book either.
Set shCompare = WB.Worksheets("Fault Comparison")

Either way, I modified your code and the filter works.
1668631682339.png



I return the two files. you should test the code with these 2 files.

 
Upvote 0
I did a test and it didn't work because in your userform code you have this line:
Set Filter_Sheets = WB.Worksheets("ConfigFilter_Sheets")


And that sheet does not exist in any of the files.

In the button you have this sheet, it doesn't exist in the book either.
Set shCompare = WB.Worksheets("Fault Comparison")

Either way, I modified your code and the filter works.
View attachment 78855


I return the two files. you should test the code with these 2 files.

Thank you for all of your help, Dante!

I ran through your sheet and compared to mine and found that my filter was not working due to the date getting converted to "custom" cell formatting instead of "General." When I transfer the date column from the original sheet to the new sheet, I originally set the values equal to each other. When the value got populated into the listbox, it put the date as a numerical value, which it would then not find in the sheet as the sheet showed the date as an actual date. Copying and pasting the dates to the new sheet resolved this issue.

Thanks again for all your help!
 
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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