Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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
Vba 如果字符串包含某个子字符串_Vba_Excel - Fatal编程技术网

Vba 如果字符串包含某个子字符串

Vba 如果字符串包含某个子字符串,vba,excel,Vba,Excel,在我的函数中,我试图检查某个字符串是否包含某个子字符串。对于非常短的字符串,它工作得非常完美,但是当我有一个带有空格的长字符串时,它似乎不再工作了 'Linear Fluorescent T(1) = "1x4 1l" T(2) = "1x4 1l basket" T(3) = "1x4 2l" 'CFL C(1) = "can- 1 4 pin" C(2) = "can- 2 2 pin" C(3) = "can- 2 4 pin" For i = 1 To 3 If InStr(Fixt

在我的函数中,我试图检查某个字符串是否包含某个子字符串。对于非常短的字符串,它工作得非常完美,但是当我有一个带有空格的长字符串时,它似乎不再工作了

'Linear Fluorescent
T(1) = "1x4 1l"
T(2) = "1x4 1l basket"
T(3) = "1x4 2l"

'CFL
C(1) = "can- 1 4 pin"
C(2) = "can- 2 2 pin"
C(3) = "can- 2 4 pin"

For i = 1 To 3
If InStr(Fixture, T(i)) >= 1 Then

    ERP_Lamp = "Linear Fluorescent"

ElseIf InStr(Fixture, C(i)) >= 1 Then

    ERP_Lamp = "Compact Fluorescent"

End If
Next i
我得到的结果总是:“线性荧光”或0。 我似乎无法得到“紧凑型荧光灯”,即使夹具是“can-14针”,它仍然会说线性荧光灯

所以问题是:如何检查我的字符串是否包含在另一个字符串“Fixture”中


提前感谢。

不是答案,但在测试中重新调整了代码并添加了一点,但本质仍然是您的,工作正常

Function ERP_Lamp(strFixture As String) As String

Dim t(3) As String
Dim c(3)   As String

t(1) = "1x4 1l"
t(2) = "1x4 1l basket"
t(3) = "1x4 2l"

'CFL
c(1) = "can- 1 4 pin"
c(2) = "can- 2 2 pin"
c(3) = "can- 2 4 pin"

strFixture = LCase(strFixture)

For i = 1 To 3

    If InStr(strFixture, LCase(t(i))) >= 1 Then

        ERP_Lamp = "Linear Fluorescent"
        Exit For

    ElseIf InStr(strFixture, LCase(c(i))) >= 1 Then

        ERP_Lamp = "Compact Fluorescent"
        Exit For

    End If

Next i

Erase t
Erase c

End Function

你能提供一些
Fixture
出错的例子吗?我刚刚用
can-1 4针
试过,效果不错。记住
InStr
是区分大小写的。使用
InStr(LCase(Fixture),LCase(T(i)),您可能会更幸运。
如何将值分配给Fixture?听起来好像它总是来自T,永远不会从C进行计算。@Chronocidal请记住,
InStr
有一个
Compare
可选参数,默认为
vbBinaryCompare
。与其使用
LCase
,不如只提供
vbTextCompare
参数并进行1次函数调用,而不是3次?