VBA - 使用通配符查找正确的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23885982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:15:59  来源:igfitidea点击:

VBA - Use wildcards to find correct string

vbams-wordwildcardword-vbaword-2010

提问by mr3k

I have a macro that use the Find function to find the string that starts with number / number number etc.. Example:
1/2313-gergre....
4/5385-gewsgergeo....

我有一个宏,它使用 Find 函数来查找以数字/数字等开头的字符串。例如:
1/2313-gergre....
4/5385-gewsgergeo....

I tried following without success:

我尝试了以下但没有成功:

    StartString = "#/#"
    With Rng.Find
    .MatchWildcards = True
        Do While .Execute(findText:=StartString, Forward:=False) = True
            MsgBox ("Found")

        Loop
    End With

It works if I use *, but I only want to accept numbers..

如果我使用 *,它会起作用,但我只想接受数字..

回答by Julian

Just do this:

只需这样做:

Set myRange = ActiveDocument.Content
StartString = "[0-9]/[0-9]"
    With myRange.Find
    .MatchWildcards = True
        Do While .Execute(findText:=StartString, Forward:=False) = True
            MsgBox ("Found")

        Loop
    End With

Here's an linkwith other wildcard options

这是其他通配符选项的链接