"不包含"功能VBA

时间:2014-07-15 13:06:20

标签: excel vba excel-vba syntax

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

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

这部分有效:

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

但我想说:

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

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

2 个答案:

答案 0 :(得分:4)

尝试使用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

答案 1 :(得分:1)

我注意到你的例子并没有找到"字"而是检查最多10个字符的字符串,并确保有九(9)个数字。如果这是您想要的,那么以下内容应该有效:

编辑:简化代码

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

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

甚至:

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

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