Home > other >  Check value after performing an InStr using VBA
Check value after performing an InStr using VBA

Time:12-14

Hi I'm currently working with a loop that checks if the currently selected cell.value is equal to the string name that I'm filtering.

But, I'm getting stuck because one word for example is TEAM, and another TEAM1, but they're both separate things. Only If I look for TEAM I also get TEAM1.

Now I was thinking of checking if the part of the string after the selected string (TEAM) is 1, but I can't really figure out how to do that, without creating different loops for every word.

What I currently have:

If InStr(1, TEAM, item) = 1 Then
   If InStr(1, TEAM1, item) = 1 Then
      Doot di doo, do this.
   Else
      Doot di doo, do this.
   End If
End If

See now I had to perform it twice, but I'd like to just be able to do it in the first if..

CodePudding user response:

TEAM is in TEAM1 but TEAM1 is not in TEAM. Simply reverse the order of the your checks should solve the problem.

If InStr(1, TEAM1, Item) = 1 Then
    Doot di doo, do this.
ElseIf InStr(1, TEAM, Item) = 1 Then
    Doot di doo, do this.
Else
    Doot di doo, do this.
End If

If there are many clause, you may want to consider using a Select Case statement:

Select Case True
Case InStr(1, TEAM1, Item) = 1
    Doot di doo, do this.
Case InStr(1, TEAM, Item) = 1
    Doot di doo, do this.
Case Else
    Doot di doo, do this.
End Select
  • Related