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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

sericom

Well-known Member
Joined
Jan 19, 2006
Messages
915
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
 

Watch MrExcel Video

Forum statistics

Threads
1,132,646
Messages
5,654,565
Members
418,140
Latest member
ahepple86

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
Top