Using a InStr function within a Select Case

流过昼夜 提交于 2019-12-30 08:36:29

问题


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


回答1:


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 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:

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



回答2:


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


来源:https://stackoverflow.com/questions/21887263/using-a-instr-function-within-a-select-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!