“不包含”函数 VBA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24759016/
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:51:40  来源:igfitidea点击:

"Does Not Contain" Function VBA

excelvbaexcel-vbasyntax

提问by user3753537

I am looking for a way to add a "Does not contain" criteria to a string. I want it to find a string that is Numeric, has a length of 9 characters, and does not contain a period "."

我正在寻找一种向字符串添加“不包含”条件的方法。我希望它找到一个数字字符串,长度为 9 个字符,并且不包含句点“。”

I've looked on the web but just found filters for a whole worksheet, or filters, or arrays, not for doesn't contain one specific character.

我在网上查看过,但只是找到了整个工作表、过滤器或数组的过滤器,而不是不包含一个特定字符的过滤器。

This part works:

这部分工作:

If IsNumeric(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)) _
    And Len(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)) = 9 Then

But I would like it to say:

但我想说:

If IsNumeric(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)) _
     And Len(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)) = 9 _
           and Doesnotcontain "." Then

What would be the proper syntax? Thanks!

什么是正确的语法?谢谢!

回答by geoB

Try the Instr() function:

试试 Instr() 函数:

If IsNumeric(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)) _
     And Len(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)) = 9 _
           and Instr(Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10), ".") = 0 Then

回答by Ron Rosenfeld

I note that your example is not looking for a "word" but rather examining a string of up to 10 characters, and ensuring there is exactly nine (9) digits. If that is what you want, then the following should work:

我注意到您的示例不是在寻找“单词”,而是检查最多 10 个字符的字符串,并确保恰好有九 (9) 位数字。如果这是你想要的,那么以下应该有效:

EDIT:Simplify the code

编辑:简化代码

S = Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10)

If S Like "#########" Then
          ... Your Routine ...
End If

or even:

甚至:

If Left(Right(wordDoc.Paragraphs(j).Range.Text, 11), 10) Like _
    "#########" Then
          ... Your Routine ...
End If

The general case of finding a "does not contain" is to use the InStr function, test for the string, and look for a return value of zero (0). Although sometimes, if the "does not contain" value is complex, use of regular expressions should be considered.

查找“不包含”的一般情况是使用 InStr 函数,测试字符串,并查找返回值零 (0)。尽管有时,如果“不包含”值很复杂,则应考虑使用正则表达式。