Possible in VBA? (Select Case)

AD_Taylor

Well-known Member
Joined
May 19, 2011
Messages
687
At the moment I have a set of code which needs to check conditions of 2 variables: trackerVers and addInVers.

Heres the code as it stands at the moment:

Code:
Sub UpdateTrackerVersion(trackerVers As Long, addInVers As Long)
'Master Sub that will call each individual update based on the version numbers it recieves
If trackerVers = 1 And addInVers = 2 Then
 
    Call UpdateVersionMod.Update_201_To_202
 
ElseIf trackerVers = 2 And addInVers = 3 Then
 
    Call UpdateVersionMod.Update_202_To_203
    
ElseIf trackerVers = 1 And addInVers = 3 Then
 
    Call UpdateVersionMod.Update_201_To_203
    
End If
End Sub

So you can understand that as more and more versions get added the code is going to get longer and longer.

I was wondering if it would be possible to restructure this into a Select Case statement so that it would be shorter and easier to read, but wasn't sure whether Select Case could handle the 'And' part?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe you could multiply one of the variables by 100 and then add them

Say if it was trackervers

If trackerVers = 1 And addInVers = 2 Then

verNo = trackerVers *100 + addInVers = 102

Then you can use VerNo for your select case statements.
 
Upvote 0
If I have understood correctly then you could use Select Case like this:
Code:
Sub UpdateTrackerVersion(trackerVers As Long, addInVers As Long)
    'Master Sub that will call each individual update based on the version numbers it recieves

    Select Case trackerVers & "|" & addInVers
        Case "1|2": Call UpdateVersionMod.Update_201_To_202
        Case "2|3": Call UpdateVersionMod.Update_202_To_203
        Case "1|3": Call UpdateVersionMod.Update_201_To_203
    End Select
End Sub



But if your called procedures are named as uniformly as they appear, then you may also be able to use this much shorter approach:
Code:
Sub UpdateTrackerVersion(trackerVers As Long, addInVers As Long)
    'Master Sub that will call each individual update based on the version numbers it recieves
    Run "UpdateVersionMod.Update_20" & trackerVers & "_To_20" & addInVers
End Sub
 
Last edited:
Upvote 0
Great suggestions, thanks both!

I've gone with Peters 'Run' suggestion as the names are uniform so this does away with the need for a separate Sub altogether.

Works fine for me so this is sorted :)
 
Upvote 0

Forum statistics

Threads
1,222,036
Messages
6,163,536
Members
451,843
Latest member
vitto

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