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

自闭症网瘾萝莉.ら 提交于 2020-07-09 02:44:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!