Lists of words for an if statement

drop05

Active Member
Joined
Mar 23, 2021
Messages
285
Office Version
  1. 365
Platform
  1. Windows
Hello, i wanted to see if there is a better way for an doing multiple if or statements then just listing if xxx=??? or xxx=???
I have an if statement like so
is there a way to get this done better, or maybe have it listed on a tab on excel and make it a reference? just so its not a large if or statement.

Thank you in advance

VBA Code:
if thisTag = "beginning date" or thisTag = "end date" or thisTag = "mid date" or thisTag = "no date" then
    if tagNum <> 0 then
       tagNum = tagNum + 1
    end if
end if
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
It depends on just how many values you might have. One way to shorten your example would be this

VBA Code:
Select Case thisTag
  Case "beginning date", "end date", "mid date", "no date"
    If tagNum <> 0 Then tagNum = tagNum + 1
  Case Else
    'If something should happen for other thisTag values then that code goes here
End Select
 
Upvote 0
It depends on just how many values you might have. One way to shorten your example would be this

VBA Code:
Select Case thisTag
  Case "beginning date", "end date", "mid date", "no date"
    If tagNum <> 0 Then tagNum = tagNum + 1
  Case Else
    'If something should happen for other thisTag values then that code goes here
End Select
i have about 10 so before i was doing the if xxx= xxx or about 9-10 times, so was trying to see if there is a shorter way
Is case similar to a list?
 
Upvote 0
i have about 10
So, with about 10 it would still be reasonable to use a 'Case' statement like I suggested above or, especially if more than 10, then if the list was in Sheet1 column E starting at E2 you could use something like this

VBA Code:
Dim rFound As Range

Set rFound = Sheets("Sheet1").Range("E2:E100").Find(What:=thisTag, LookAt:=xlWhole, MatchCase:=True)
If Not rFound Is Nothing And tagNum <> 0 Then tagNum = tagNum + 1
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top