Using a InStr function within a Select Case
excel, excel-2010, vba
Solution
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
Problem
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