开发者

Expression to test for one or more spaces in VBA to find matches in Excel cells

开发者 https://www.devze.com 2023-04-12 15:49 出处:网络
I am trying to use regular expressions in a macro for an Excel document. Here is what I have so far:

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 tryin开发者_StackOverflow中文版g 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.


You need to create the regexp object, ie

Dim regex
Set regex = CreateObject("vbscript.regexp")
regex.Pattern = "\s+"
0

精彩评论

暂无评论...
验证码 换一张
取 消