问题
I am trying to use regular expressions in a macro for an Excel document.
Here is what I have so far:
dim regex
regex.pattern = "\s+"
for p = 3 to totalparamcount
if (regex.test(Worksheets("table").Cells(p,1)) then
msgbox ("blah blah")
end if
next
The pattern I am trying to match is one or more spaces, tabs, newlines, which I saw is denoted by "\s"
.
The line 'if (regex.test(Worksheets("table").Cells(p,1)) then
' is getting this error:
Run-time error '424':
Object required
I am using VBA 7.0 in Excel.
回答1:
You need to create the regexp object, ie
Dim regex
Set regex = CreateObject("vbscript.regexp")
regex.Pattern = "\s+"
来源:https://stackoverflow.com/questions/7777419/expression-to-test-for-one-or-more-spaces-in-vba-to-find-matches-in-excel-cells