VBA - help troubleshooting autofilter code

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi guys,

I have this code and all is working except if there is blank in the criteria range.

2 things:

1) From the code blow, on sheet1 the worksheetfunction.count, if the range is blank and nothing entered, my filtered results in blank. Can not figure how to fix this.
2) I want to add another criteria to filter out any entries that is within the last 7 days on Sheet2, field:=8

VBA Code:
Sub filter()
On Error Resume Next
Dim scount As Integer, fcount As Integer, shcount As Integer, dcount As Integer, stcount As Integer, tcount As Integer, dacount As Integer
Dim sitelist As Variant, fclmlist As Variant, shiftlist As Variant, deptlist As Variant, statuslist As Variant, typelist As Variant, dalist As Variant

Sheet1.Activate
scount = WorksheetFunction.CountA(Range("H5", Range("H5").End(xlDown)))
shcount = WorksheetFunction.CountA(Range("I5", Range("I5").End(xlDown)))
dcount = WorksheetFunction.CountA(Range("J5", Range("J5").End(xlDown)))
fcount = WorksheetFunction.CountA(Range("K5", Range("K5").End(xlDown)))
stcount = WorksheetFunction.CountA(Range("L5", Range("L5").End(xlDown)))
tcount = WorksheetFunction.CountA(Range("M5", Range("M5").End(xlDown)))

Set scell = ThisWorkbook.Worksheets("Dashboard").Range("H5")
Set shcell = ThisWorkbook.Worksheets("Dashboard").Range("I5")
Set dcell = ThisWorkbook.Worksheets("Dashboard").Range("J5")
Set fcell = ThisWorkbook.Worksheets("Dashboard").Range("K5")
Set stcell = ThisWorkbook.Worksheets("Dashboard").Range("L5")
Set tcell = ThisWorkbook.Worksheets("Dashboard").Range("M5")


    sitelist = Range(Cells(5, 8), Cells(scount, 8)).Value
    sitelist = Application.Transpose(sitelist)
    sitelist = Join(sitelist, ",")
    sitelist = Split(sitelist, ",")
   
    shiftlist = Range(Cells(5, 9), Cells(shcount, 9)).Value
    shiftlist = Application.Transpose(shiftlist)
    shiftlist = Join(shiftlist, ",")
    shiftlist = Split(shiftlist, ",")
   
    deptlist = Range(Cells(5, 10), Cells(dcount, 10)).Value
    deptlist = Application.Transpose(deptlist)
    deptlist = Join(deptlist, ",")
    deptlist = Split(deptlist, ",")
   
    fclmlist = Range(Cells(5, 11), Cells(fcount, 11)).Value
    fclmlist = Application.Transpose(fclmlist)
    fclmlist = Join(fclmlist, ",")
    fclmlist = Split(fclmlist, ",")
   
    statuslist = Range(Cells(5, 12), Cells(stcount, 12)).Value
    statuslist = Application.Transpose(statuslist)
    statuslist = Join(statuslist, ",")
    statuslist = Split(statuslist, ",")
   
    typelist = Range(Cells(5, 13), Cells(tcount, 13)).Value
    typelist = Application.Transpose(typelist)
    typelist = Join(typelist, ",")
    typelist = Split(typelist, ",")
      
    With Sheet2
        .Range("A1:I1").AutoFilter field:=1, Criteria1:=sitelist
        .Range("A1:I1").AutoFilter field:=3, Criteria1:=statuslist
        .Range("A1:I1").AutoFilter field:=4, Criteria1:=shiftlist
        .Range("A1:I1").AutoFilter field:=5, Criteria1:=deptlist
        .Range("A1:I1").AutoFilter field:=6, Criteria1:=fclmlist
        .Range("A1:I1").AutoFilter field:=7, Criteria1:=typelist
    End With

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Please save a copy of your code current workbook & code and then try try this:
  • The count formulas seem to set the wrong last row number so I have changed them
  • The join and split lines seem unnecessary
  • The transpose returns a zero when the criteria range is empty hence the addition of
    "if criteria <> 0 then apply filter"
  • An array filter seems to need the: Operator:=xlFilterValues
  • **** right at the end I have put a line to accommodate your date request ****
    • I did not know which field is your date field so you will need to change the field no
    • you will also need to check if you want >= I currently have just >
    • The line is:-
      .AutoFilter field:=6, Criteria1:=">" & (CLng(Date) - 7)

VBA Code:
Sub filter()
    On Error Resume Next
    Dim scount As Integer, fcount As Integer, shcount As Integer, dcount As Integer, stcount As Integer, tcount As Integer, dacount As Integer
    Dim sitelist As Variant, fclmlist As Variant, shiftlist As Variant, deptlist As Variant, statuslist As Variant, typelist As Variant, dalist As Variant
    ' ADDED
    Dim scell As Range, shcell As Range, dcell As Range, fcell As Range, stcell As Range, tcell As Range
    
    ' CHANGED
    Sheet1.Activate
    scount = Range("H5").End(xlDown).Row
    shcount = Range("I5").End(xlDown).Row
    dcount = Range("J5").End(xlDown).Row
    fcount = Range("K5").End(xlDown).Row
    stcount = Range("L5").End(xlDown).Row
    tcount = Range("M5").End(xlDown).Row
    
    Set scell = ThisWorkbook.Worksheets("Dashboard").Range("H5")
    Set shcell = ThisWorkbook.Worksheets("Dashboard").Range("I5")
    Set dcell = ThisWorkbook.Worksheets("Dashboard").Range("J5")
    Set fcell = ThisWorkbook.Worksheets("Dashboard").Range("K5")
    Set stcell = ThisWorkbook.Worksheets("Dashboard").Range("L5")
    Set tcell = ThisWorkbook.Worksheets("Dashboard").Range("M5")
       
    sitelist = Range(Cells(5, 8), Cells(scount, 8)).Value
    sitelist = Application.Transpose(sitelist)

    shiftlist = Range(Cells(5, 9), Cells(shcount, 9)).Value
    shiftlist = Application.Transpose(shiftlist)

    deptlist = Range(Cells(5, 10), Cells(dcount, 10)).Value
    deptlist = Application.Transpose(deptlist)
  
    fclmlist = Range(Cells(5, 11), Cells(fcount, 11)).Value
    fclmlist = Application.Transpose(fclmlist)
    
    statuslist = Range(Cells(5, 12), Cells(stcount, 12)).Value
    statuslist = Application.Transpose(statuslist)
    
    typelist = Range(Cells(5, 13), Cells(tcount, 13)).Value
    typelist = Application.Transpose(typelist)

    Sheet2.ShowAllData
    With Sheet2.Range("A1:I1")
        If sitelist <> 0 Then .AutoFilter field:=1, Criteria1:=sitelist, Operator:=xlFilterValues
        If statuslist <> 0 Then .AutoFilter field:=3, Criteria1:=statuslist, Operator:=xlFilterValues
        If shiftlist <> 0 Then .AutoFilter field:=4, Criteria1:=shiftlist, Operator:=xlFilterValues
        If deptlist <> 0 Then .AutoFilter field:=5, Criteria1:=deptlist, Operator:=xlFilterValues
        'If fclmlist <> 0 Then .AutoFilter field:=6, Criteria1:=fclmlist, Operator:=xlFilterValues
        If typelist <> 0 Then .AutoFilter field:=7, Criteria1:=typelist, Operator:=xlFilterValues
         
' XXXXX Change the Field to be whatever the date field is XXXXX
         .AutoFilter field:=6, Criteria1:=">" & (CLng(Date) - 7)
     End With

End Sub
 
Upvote 0
Thank you for helping, I ran into an issue,

When I got to filter the deptlist and fclmlist, its giving me blank. My mind is boggled as to why. Everything else filtered correctly, even the date.

Below is my list of criteria.
Quebec Call'em All.xlsb
HIJKLM
5SiteShift PatternDepartmentFCLM AreaStatusType
6DB3C070012990103ActiveHourly
71299030
8
Dashboard


Below is the list that needs to be filtered.
Quebec Call'em All.xlsb
ABCDEFGHI
1Location: Location NameEmployee IdPayroll Employee StatusShift PatternDepartmentFCLM AreaEmployee TypeLast Hire DateMobile Phone - Private
2ActiveNA6C181512990103Hourly7/2/2020
3ActiveACCCOM12990703Hourly7/2/2020
4ActiveNL4C183012990603Hourly8/5/2020
5ActiveNA5C18151299405Hourly7/2/2020
6ActiveNB3C1845129903013Hourly8/2/2020
7ActiveNN4C1900129903013Hourly8/3/2020
8ActiveNB2C183012990103Hourly7/5/2020
9ActiveNB2C183012990103Hourly7/5/2020
Roster
 
Upvote 0
It is still quite early here in Australia so I will modify the code later today.
I think you just told me what the split/join does. The 2 that are failing are numeric and need to be text. You can test it by putting back the split/join for those columns if you want to try it.
The If statement will need to be modified though.
I also want change the xldown lines to starting at rows.count and using xlup, to cover off having an empty list.
 
Upvote 0
It is still quite early here in Australia so I will modify the code later today.
I think you just told me what the split/join does. The 2 that are failing are numeric and need to be text. You can test it by putting back the split/join for those columns if you want to try it.
The If statement will need to be modified though.
I also want change the xldown lines to starting at rows.count and using xlup, to cover off having an empty list.
This worked.

modified to rows.count and added back the join/split code and now it filters those 2 columns correct. Thank you
VBA Code:
    scount = ActiveSheet.Cells(Rows.count, "H").End(xlUp).Row
    shcount = ActiveSheet.Cells(Rows.count, "I").End(xlUp).Row
    dcount = ActiveSheet.Cells(Rows.count, "J").End(xlUp).Row
    fcount = ActiveSheet.Cells(Rows.count, "K").End(xlUp).Row
    dacount = ActiveSheet.Cells(Rows.count, "N").End(xlUp).Row

Thank you for all the help.
 
Upvote 0
Thanks for the update.

There are some other changes that you might want to include. (Look for XXXXX in the code)
  • if there are no criteria in a criteria column the xlup will overshoot what is currently supposed to be the first row of the filter range namely row 5.
    So added a check that if xlup is giving a row no < than your first filter data row (currently 5) to use the firstRow instead
    eg If scount < firstRow Then scount = firstRow

  • Since there is now a variable for the firstRow I changed all the list variables to use that as the row reference for the starting cell.
    eg shiftlist = Range(Cells(firstRow, 9), Cells(shcount, 9)).Value

  • For your numeric fields (deptlist & fclmlist) if they only contain 1 value it is not converting to a string which means it doesn't work in the filter. So added this line to convert non-empty single value non-array variable to a string.
    If Not IsArray(deptlist) And deptlist <> 0 Then deptlist = CStr(deptlist)

  • I don't know what field you are using for your date filter so it is currently a line at the end that is commented out.
VBA Code:
Sub filter()
    On Error Resume Next
    Dim scount As Integer, fcount As Integer, shcount As Integer, dcount As Integer, stcount As Integer, tcount As Integer, dacount As Integer
    Dim sitelist As Variant, fclmlist As Variant, shiftlist As Variant, deptlist As Variant, statuslist As Variant, typelist As Variant, dalist As Variant

    Dim scell As Range, shcell As Range, dcell As Range, fcell As Range, stcell As Range, tcell As Range
    
    Sheet1.Activate
    scount = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row
    shcount = ActiveSheet.Cells(Rows.Count, "I").End(xlUp).Row
    dcount = ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Row
    fcount = ActiveSheet.Cells(Rows.Count, "K").End(xlUp).Row
    dacount = ActiveSheet.Cells(Rows.Count, "N").End(xlUp).Row
    
    Set scell = ThisWorkbook.Worksheets("Dashboard").Range("H5")
    Set shcell = ThisWorkbook.Worksheets("Dashboard").Range("I5")
    Set dcell = ThisWorkbook.Worksheets("Dashboard").Range("J5")
    Set fcell = ThisWorkbook.Worksheets("Dashboard").Range("K5")
    Set stcell = ThisWorkbook.Worksheets("Dashboard").Range("L5")
    Set tcell = ThisWorkbook.Worksheets("Dashboard").Range("M5")
       
    ' XXXX ADDED XXXX
    Dim firstRow As Long
    firstRow = 5
    If scount < firstRow Then scount = firstRow
    If shcount < firstRow Then shcount = firstRow
    If dcount < firstRow Then dcount = firstRow
    If fcount < firstRow Then fcount = firstRow
    If dacount < firstRow Then dacount = firstRow
    
    ' XXXX CHANGED XXXX
    sitelist = Range(Cells(firstRow, 8), Cells(scount, 8)).Value
    sitelist = Application.Transpose(sitelist)

    shiftlist = Range(Cells(firstRow, 9), Cells(shcount, 9)).Value
    shiftlist = Application.Transpose(shiftlist)

    deptlist = Range(Cells(firstRow, 10), Cells(dcount, 10)).Value
    deptlist = Application.Transpose(deptlist)
    deptlist = Split(Join(deptlist, ","), ",")
    If Not IsArray(deptlist) And deptlist <> 0 Then deptlist = CStr(deptlist)
  
    fclmlist = Range(Cells(firstRow, 11), Cells(fcount, 11)).Value
    fclmlist = Application.Transpose(fclmlist)
    fclmlist = Split(Join(fclmlist, ","), ",")
    If Not IsArray(fclmlist) And fclmlist<> 0 Then fclmlist = CStr(fclmlist)
    
    statuslist = Range(Cells(firstRow, 12), Cells(stcount, 12)).Value
    statuslist = Application.Transpose(statuslist)
    
    typelist = Range(Cells(firstRow, 13), Cells(tcount, 13)).Value
    typelist = Application.Transpose(typelist)
    ' XXXXX END OF LATEST CHANGES XXXXX

    Sheet2.ShowAllData
    With Sheet2.Range("A1:I1")
        If sitelist <> 0 Then .AutoFilter field:=1, Criteria1:=sitelist, Operator:=xlFilterValues
        If statuslist <> 0 Then .AutoFilter field:=3, Criteria1:=statuslist, Operator:=xlFilterValues
        If shiftlist <> 0 Then .AutoFilter field:=4, Criteria1:=shiftlist, Operator:=xlFilterValues
        If deptlist <> 0 Then .AutoFilter field:=5, Criteria1:=deptlist, Operator:=xlFilterValues
        If fclmlist <> 0 Then .AutoFilter field:=6, Criteria1:=fclmlist, Operator:=xlFilterValues
        If typelist <> 0 Then .AutoFilter field:=7, Criteria1:=typelist, Operator:=xlFilterValues
         
' XXXXX Change the Field to be whatever the date field is XXXXX
         '.AutoFilter field:=6, Criteria1:=">" & (CLng(Date) - 7)
     End With

End Sub
 
Upvote 0
Solution
Follow up question, I noticed that when I use 1 wildcard criteria, the data filters correctly but when I have 2 in the same column, its returning 0 results.
i.e. on the Shift Pattern (column I), when I enter PT* (for any part time shifts) and DA* (Morning shifts sun-wed), my Roster tab would filter and I see blanks. But having only 1, it would filter fine. I can not seem to figure what is causing this.
 
Upvote 0
I had a look at this and if you only have 2 items in a criteria array and they have wildcards in it, then it will work.

If you put wildcards in the selection array it switches from a "selected items filter" to a "custom filter" (either begins with, ends with or contains, depending on where you put the wildcards)

The custom filter only allows for 2 criteria.
The suggestions are:
  • Use advanced filter
    With so many criteria combinations as you have I don't think that would work well for you.
  • Use a helper column
This range criteria
1623328475396.png


Generated this Custom Filter
1623327950715.png


Without the wildcards it generates this selected items filter

1623328602851.png
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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