vba 在 Select Case 中使用 InStr 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21887263/
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
Using a InStr function within a Select Case
提问by user2385809
This does not seem to be working. Is there a way to do what I am trying to do here? I cant a case to be selected if the value is in a given sting:
这似乎不起作用。有没有办法做我在这里想做的事情?如果值在给定的范围内,我无法选择案例:
Select Case gTTD.Cells(r, 4)
Case InStr(gTTD.Cells(r, 4), "MASTER LOG")
resp = "MM LOG"
Case InStr(gTTD.Cells(r, 4), "MASTER MET")
resp = "MM MET"
Case "PIR"
gTTD.Cells(r, 7) = "Martin Tr??panier"
resp = "Martin Tr??panier"
End Select
I understand why this cant work but is there a way of making it work? thank you
我明白为什么这不能工作,但有没有办法让它工作?谢谢你
Thank you
谢谢
回答by Steve
Here is a little trick I use, the select statement just wants to find results that are the same. Here is a simple example:
这是我使用的一个小技巧,select 语句只是想找到相同的结果。这是一个简单的例子:
Select Case True
Case (1 = 2)
Stop
Case (2 = 3)
Stop
Case (4 = 4)
Stop
Case Else
Stop
End Select
This will fall into the 4=4 case. In your example, this might be the simple answer:
这将属于 4=4 的情况。在您的示例中,这可能是简单的答案:
Select Case True
Case (InStr(gTTD.Cells(r, 4), "MASTER LOG") > 0)
resp = "MM LOG"
Case (InStr(gTTD.Cells(r, 4), "MASTER MET") > 0)
resp = "MM MET"
Case else
gTTD.Cells(r, 7) = "Martin Tr??panier"
resp = "Martin Tr??panier"
End Select
回答by Pedrumj
try this
尝试这个
dim r as integer
for r = 1 to 'number of rows
if Instr(gTTD.Cells(r, 4), "MASTER LOG") <> 0 then
resp = "MM LOG"
elseif Instr(gTTD.Cells(r, 4), "MASTER MET") <> 0 then
resp = "MM MET"
else
gTTD.Cells(r, 7) = "Martin Tr??panier"
resp = "Martin Tr??panier"
end if
next r