btorrean
Board Regular
- Joined
- Dec 2, 2011
- Messages
- 76
Dear Forum Members,
I apologize if I'm missing something really simple here, but I'm trying to use VBA to test whether a string contains only zeroes. The string can be many zeroes, or one zero, but must only contain zeroes. Here's the code I've been trying. It always tells me that the string has zeroes, even if some of the characters are not zeroes. If the string contains no zeroes, it will tell me that it does not contain zeroes, however. I think that my problem is with the pattern. I've not used RegEx before, so I'm not super-familiar with how to write the patterns. Can anyone help? Thanks!
Brian
I apologize if I'm missing something really simple here, but I'm trying to use VBA to test whether a string contains only zeroes. The string can be many zeroes, or one zero, but must only contain zeroes. Here's the code I've been trying. It always tells me that the string has zeroes, even if some of the characters are not zeroes. If the string contains no zeroes, it will tell me that it does not contain zeroes, however. I think that my problem is with the pattern. I've not used RegEx before, so I'm not super-familiar with how to write the patterns. Can anyone help? Thanks!
Code:
Option Explicit
Option Compare Text
Public Sub TestRegEx()
Dim myNumStr As String
Dim regEx As VBScript_RegExp_55.RegExp
Dim strPattern As String
myNumStr = "0100" ' This does NOT produce an error, and it should.
Set regEx = New VBScript_RegExp_55.RegExp
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "[0]+"
End With
If regEx.test(myNumStr) Then
MsgBox "The string '" & myNumStr & "' IS all zeroes!! Congrats!!", vbInformation
Else
MsgBox "The string '" & myNumStr & "' is NOT all zeroes. Bummer dude!", vbCritical
End If
Set regEx = Nothing
End Sub
Brian