Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在没有空格的字符串中识别子字符串_Excel_Vba - Fatal编程技术网

Excel 如何在没有空格的字符串中识别子字符串

Excel 如何在没有空格的字符串中识别子字符串,excel,vba,Excel,Vba,我希望检查子字符串PROD是否存在于字符串今天的产品中,并返回值True或False 在这里,预期的答案是正确的 如何在VBA中执行此操作像使用一样使用 Sub testString() Dim myStr As String myStr = "Today's production" MsgBox myStr Like "*prod*" End Sub 如果您希望得到不区分大小写的结果,则需要在模块顶部使用选项Compare Text,因为默认的比较方法是比较二

我希望检查子字符串
PROD
是否存在于字符串
今天的产品中
,并返回值True或False

在这里,预期的答案是正确的

如何在VBA中执行此操作

像使用
一样使用

Sub testString()

    Dim myStr As String

    myStr = "Today's production"

    MsgBox myStr Like "*prod*"

End Sub
如果您希望得到不区分大小写的结果,则需要在模块顶部使用
选项Compare Text
,因为默认的比较方法是比较二进制(区分大小写)

如果您不想检查是否区分大小写,则最好使用由建议的更简单的方法

Sub testString()

    Dim myStr As String

    myStr = "Today's proDuction"

    MsgBox InStr(1, myStr, "prod", vbTextCompare) > 0

End Sub
一样使用

Sub testString()

    Dim myStr As String

    myStr = "Today's production"

    MsgBox myStr Like "*prod*"

End Sub
如果您希望得到不区分大小写的结果,则需要在模块顶部使用
选项Compare Text
,因为默认的比较方法是比较二进制(区分大小写)

如果您不想检查是否区分大小写,则最好使用由建议的更简单的方法

Sub testString()

    Dim myStr As String

    myStr = "Today's proDuction"

    MsgBox InStr(1, myStr, "prod", vbTextCompare) > 0

End Sub
我原来的答覆是:

Instr(Ucase("Today's proDuction"), "PROD") > 0

编辑: 根据@GSerg下面的评论和@KDavis的回答,我认为值得调查和比较不同的方法。有趣的是,结果并不是我所期望的。以下是我的测试代码:

Option Explicit
Option Compare Text

Sub A()

    Dim i As Long
    Dim res As Boolean

    '// Timer class courtesy of @Mike Woodhouse here:
    '// https://stackoverflow.com/questions/198409/how-do-you-test-running-time-of-vba-code
    Dim tt As cTimer

    Set tt = New cTimer

    '// First test using UCASE to convert text with default comparison mode
    Debug.Print "A " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        '// Need to state binary compare explicitly to override Option Compare Text above
        res = InStr(1, UCase("Today's proDuction"), "PROD", vbBinaryCompare) > 0
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"


    '// Second test using vbTextCompare comparison mode and implicit type conversion
    Debug.Print "B " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = InStr(1, "Today's proDuction", "PROD", vbTextCompare)
    Next i


    Debug.Print " [" & tt.TimeElapsed & "]"


    '// Third test using vbTextCompare comparison mode and explicit test to convert to boolean
    Debug.Print "C " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = InStr(1, "Today's proDuction", "PROD", vbTextCompare) > 0
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"



    '// Fourth test using vbTextCompare comparison mode and explicit type conversion
    Debug.Print "D " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = CBool(InStr(1, "Today's proDuction", "PROD", vbTextCompare))
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"

    '// Fourth test using like
    Debug.Print "E " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = "Today's proDuction" Like "*PROD*"
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"
End Sub
这是一个典型的结果,在许多测试运行中都非常一致:

A 28/02/2018 14:36:29 [3479.85848592479]
B 28/02/2018 14:36:33 [5145.24250798252]
C 28/02/2018 14:36:38 [5159.0118225338]
D 28/02/2018 14:36:43 [6627.32650697809]
E 28/02/2018 14:36:50 [6042.61252265476]
因此,似乎转换为大写,然后使用带有默认比较模式的INSTR比使用INSTR的vbTextCompare比较模式和使用带有通配符的
类似的
语法要快得多

当然,在大多数情况下,性能上的差异不太可能明显。

我的原始答案:

Instr(Ucase("Today's proDuction"), "PROD") > 0

编辑: 根据@GSerg下面的评论和@KDavis的回答,我认为值得调查和比较不同的方法。有趣的是,结果并不是我所期望的。以下是我的测试代码:

Option Explicit
Option Compare Text

Sub A()

    Dim i As Long
    Dim res As Boolean

    '// Timer class courtesy of @Mike Woodhouse here:
    '// https://stackoverflow.com/questions/198409/how-do-you-test-running-time-of-vba-code
    Dim tt As cTimer

    Set tt = New cTimer

    '// First test using UCASE to convert text with default comparison mode
    Debug.Print "A " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        '// Need to state binary compare explicitly to override Option Compare Text above
        res = InStr(1, UCase("Today's proDuction"), "PROD", vbBinaryCompare) > 0
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"


    '// Second test using vbTextCompare comparison mode and implicit type conversion
    Debug.Print "B " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = InStr(1, "Today's proDuction", "PROD", vbTextCompare)
    Next i


    Debug.Print " [" & tt.TimeElapsed & "]"


    '// Third test using vbTextCompare comparison mode and explicit test to convert to boolean
    Debug.Print "C " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = InStr(1, "Today's proDuction", "PROD", vbTextCompare) > 0
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"



    '// Fourth test using vbTextCompare comparison mode and explicit type conversion
    Debug.Print "D " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = CBool(InStr(1, "Today's proDuction", "PROD", vbTextCompare))
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"

    '// Fourth test using like
    Debug.Print "E " & Now();

    tt.StartCounter

    For i = 1 To 10000000
        res = "Today's proDuction" Like "*PROD*"
    Next i

    Debug.Print " [" & tt.TimeElapsed & "]"
End Sub
这是一个典型的结果,在许多测试运行中都非常一致:

A 28/02/2018 14:36:29 [3479.85848592479]
B 28/02/2018 14:36:33 [5145.24250798252]
C 28/02/2018 14:36:38 [5159.0118225338]
D 28/02/2018 14:36:43 [6627.32650697809]
E 28/02/2018 14:36:50 [6042.61252265476]
因此,似乎转换为大写,然后使用带有默认比较模式的INSTR比使用INSTR的vbTextCompare比较模式和使用带有通配符的
类似的
语法要快得多


当然,在大多数情况下,性能上的差异不太可能明显。

如果要确定一个子字符串是否在另一个字符串中,请使用InStr。如果要确定一个子字符串是否在另一个字符串中,请使用InStr。
InStr
支持比较模式。使用
UCase
LCase
都是错误的。是的,转换为大写更快。如果您接受允许您的代码出错,那么您可以通过不做任何事情并返回.Hmmm来进一步推动它,使它无限快@GSerg是的,重点是,你是对的,这在某些地区可能不起作用。但在某些情况下接受错误的代码并不等于接受总是错误的代码。
InStr
支持比较模式。使用
UCase
LCase
都是错误的。是的,转换为大写更快。如果您接受允许您的代码出错,那么您可以通过不做任何事情并返回.Hmmm来进一步推动它,使它无限快@GSerg是的,重点是,你是对的,这在某些地区可能不起作用。但在某些情况下接受错误的代码并不等同于接受总是错误的代码。