I have a userform that uses a checkbox to filter out blanks for a certain column. The range to be filtered is Sheet 1 A1:C10, and the criteria range is Sheet 2 A1:C2.
If checked: Don't filter Column C If unchecked: Filter out blanks on Column C
I have looked around and found that the operator to filter blanks on an advanced filter is "=". Therefore, when I process the filter when I press a button on the userform, I set Sheet 2 C2 to "="
If Checkbox1.Value Then
Sheets(2).Range("C2").Value = ""
Else
Sheets(2).Range("C2").Value = "="
End If
Sheets(1).Range("A1:C10").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets(2).Range("A1:C2"), Unique:=False
When I process this, all entries disappear, even blanks. The same is true if I use:
Else
Sheets(2).Range("C2").Value = "<>"
This should show non-blanks, but it hides all rows.
Here's the kicker; to troubleshoot, I recorded a macro of me auto-filtering blanks, and it gave me this:
ActiveSheet.Range("$A$1:$C$2").AutoFilter Field:=3, Criteria1:="="
So, it seems that filtering "=" is correct, but it just doesn't want to work with me.
Side note: just for kicks, I thought that the criteria cell needed to show ="", which means that the formula would read ="=""""", which means that the VBA would read:
Else
Sheets(2).Range("C2").Value = "=""="""""""""""
This also does not work.
Edit: @Scott, I have the following four lines. None of which seem to work. The linked page shows an example to remove blanks, not exclusively show them. Therefore I tried =0 in addition to >0.
Else
Sheets(2).Range("C2").Value = "=LEN(Sheet1!C2)=0"
Else
Sheets(2).Range("C2").Value = "=""=LEN(Sheet1!C2)=0"""
Else
Sheets(2).Range("C2").Value = "=LEN(Sheet1!C2)>0"
Else
Sheets(2).Range("C2").Value = "=""=LEN(Sheet1!C2)>0"""
Okay we missed a step.
C1 on sheet2 needs to be empty.
The formula will take care of the filter part.
Then the code is:
If Checkbox1.Value Then
Sheets(2).Range("C2").Value = ""
Else
Sheets(2).Range("C2").Formula = "=LEN(Sheet1!C2)=0"
End If
Sheets(1).Range("A1:C10").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Sheets(2).Range("A1:C2"), Unique:=False