How to search for phones with Autofilter in VBA

Asked

Viewed 48 times

0

I am using VBA to apply a filter in the phone numbers column.

The phone numbers are treated in a personalized way to stay in the model (XX) XXXXX-XXXX or (XX) XXXX-XXXX

I take the value of a cell and use as the value for the filter search.

Follows the code:

If Not IsEmpty(Range("B7")) Then
Range("A12").AutoFilter Field:=2, Criteria1:=Range("B7").Value

End If

I get no return when the value is formatted

  • What behavior did you expect? The code does not include anything related to being or not formatted in X or Y form. What this code snippet does is just the following: if cell B7 is not empty, go to the existing filter that covers cell A12, take the second column and configure to show only the values equivalent to B7. Does it behave any other way than this? Gives some error message?

  • Also, a "=" is missing in the parameter Criteria1. Was supposed to be Criteria1:=Range("B7").Value

  • @Césarrodriguez is correct! I’m actually trying to find a way to do the search to filter the values with custom formatting, I tried using Numberformat but also did not get result because it was returning FALSE and not the formatted number. The result I get is a filter without the symbols that are used in custom formatting, but I haven’t found a way to solve this yet.

1 answer

0


After a little more research I found this solution using the Strings.Format, he returns a string then in this way the VBA passes the exact value with all the symbols in the AutoFilter.

Dim NumTelefone As String
NumTelefone = Range("B7")
If Len(NumTelefone) > 10 Then
NumTelefone = Strings.Format(NumTelefone, "[<=9999999999] (##)#####-####")
Else
NumTelefone = Strings.Format(NumTelefone, "[<=9999999999] (##)####-####")
End If

Browser other questions tagged

You are not signed in. Login or sign up in order to post.