String InStr找不到子字符串
我有以下代码:String InStr找不到子字符串,string,excel,vba,contains,select-case,String,Excel,Vba,Contains,Select Case,我有以下代码: Columns("F:F").Select For Each cell In Selection Select Case cell Case InStr(1, cell, ",") <> 0 MsgBox ("found") Case IsEmpty(cell) Or Null MsgBox ("empty") Case Else Stop
Columns("F:F").Select
For Each cell In Selection
Select Case cell
Case InStr(1, cell, ",") <> 0
MsgBox ("found")
Case IsEmpty(cell) Or Null
MsgBox ("empty")
Case Else
Stop
End Select
Next
在F:F列中,我按顺序排列如下:Marc Jacobs,Renolds,Bob InStr没有找到任何正确的案例陈述
对于Marc Jacobs,我得到了其他人的正确答案
对于,我得到的消息应该是空消息
对于雷诺德,鲍勃,我收到了这个案子,其他电话应该收到找到的消息
这是怎么回事?您使用选择大小写语法的方式似乎是问题所在。每个案例都包含一个计算。因此,VB将在进行选择案例比较之前对每个案例进行计算 例如:对于第一个循环,单元格=Renolds,Bob。因此,您的第一个案例条件将InStr1,cell,,计算为8,并将80计算为True。但是,Renolds,Bob不等于InStr1,cell,,0',后者等于True。奇怪的是,当VBA转换为布尔值以便进行比较时,会转换为True 你可能想写的是
For Each cell in Selection
If InStr(1, cell, ",") <> 0 Then
MsgBox("found")
ElseIf IsEmpty(cell)
MsgBox("empty")
Else
Stop
End If
Next
您使用Select Case语法的方式似乎是问题所在。每个案例都包含一个计算。因此,VB将在进行选择案例比较之前对每个案例进行计算 例如:对于第一个循环,单元格=Renolds,Bob。因此,您的第一个案例条件将InStr1,cell,,计算为8,并将80计算为True。但是,Renolds,Bob不等于InStr1,cell,,0',后者等于True。奇怪的是,当VBA转换为布尔值以便进行比较时,会转换为True 你可能想写的是
For Each cell in Selection
If InStr(1, cell, ",") <> 0 Then
MsgBox("found")
ElseIf IsEmpty(cell)
MsgBox("empty")
Else
Stop
End If
Next
您也可以使用Select…Case语句:
For Each cell In Selection
Select Case True
Case InStr(1, cell, ",") <> 0
MsgBox ("found")
Case IsEmpty(cell)
MsgBox ("empty")
Case Else
Stop
End Select
Next
您也可以使用Select…Case语句:
For Each cell In Selection
Select Case True
Case InStr(1, cell, ",") <> 0
MsgBox ("found")
Case IsEmpty(cell)
MsgBox ("empty")
Case Else
Stop
End Select
Next