VBA Regex Not Recognizing Pattern

bt_24

New Member
Joined
Jan 16, 2017
Messages
19
Hi,

I'm working on a program that I want to recognize if a cell contains any number in it by leveraging a RegEx. Currently it isn't working and only returns text when the cell has numbers and text. In the cell I have "55112 5 mile radius" & the cell format is listed as general. Would love any insight here....

VBA Code:
Option Explicit
Sub workarea()
Dim RegNew As RegExp 
 Set RegNew = New RegExp
    With RegNew
        .Pattern = "([^0-9])" 
        .Global = False
        .IgnoreCase = True
     End With

    If RegNew.Test(ActiveWorkbook.ActiveSheet.Cells(3, 4)) = True Then
         Debug.Print "no numbers in the file"
          Else
          Debug.Print "numbers found"
    End If
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Give this a try, had to swap your logic in the 'If' statement.

VBA Code:
Sub workarea()
    Dim RegNew As RegExp
    Set RegNew = New RegExp
    With RegNew
        .Pattern = "\d"
        .Global = False
        .IgnoreCase = True
    End With
    
    If RegNew.test(ActiveWorkbook.ActiveSheet.Cells(3, 4).Value) Then
        Debug.Print "numbers found"
    Else
        Debug.Print "no numbers in the file"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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