excelvbaautofilter

Autofilter values contains the Asterisk (*)


On Excel, I can use tilde (~) before Asterisk (*) to filter of values contains the asterisk (*) and it works.
I tried the same on VBA, But It didn't work at all (the result is null values).
I have many values contains the asterisk (*) as a string like ( 8*16 , 10*11).
kindly , how to fix this issue without replacing the asterisk on my main dataset.

Option Explicit
Option Compare Text

Sub AutoFilter_on_Asterisk()

    Dim ws As Worksheet, rng As Range
     Set ws = ActiveSheet
      Set rng = ws.Range("A3:AH7000")
    
    rng.AutoFilter field:=5, Criteria1:="~*", Operator:=xlFilterValues  'the result is null values
    
End Sub

Solution

  • The tilde "escapes" the asterix character to signal that you don't want to use it as wildcard, but as character. If you enter X as criteria, Excel will show you all rows that are exactly X. If you want to get all rows where the value contains an X, you can either enter *X* in the search field, or you use the "TextFilters->Contains..." dialog (which will do the same search, only that you don't need to know that the asterix is the wildcard character).

    Searching for an asterix is exactly the same: Your code searches for rows that contain only an asterix (and as you don't have any, your result is empty). Your search text needs to be *~**:

    rng.AutoFilter field:=5, Criteria1:="*~**", Operator:=xlFilterValues