Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading