andrewb90
Well-known Member
- Joined
- Dec 16, 2009
- Messages
- 1,077
Hi all,
I have a number of similarly named user forms that are called based on the value in a cell. I am trying to find a more efficient way to call the user forms without having to add additional elseifs to the code everytime I add a few more. I thought I had the right idea, but perhaps I am not using my variables correctly.
This code is the small portion that I currently have and it works, but I'd like to eliminate the need to add additional statements to the IF:
Any help in properly correcting this code would be much appreciated!
I have a number of similarly named user forms that are called based on the value in a cell. I am trying to find a more efficient way to call the user forms without having to add additional elseifs to the code everytime I add a few more. I thought I had the right idea, but perhaps I am not using my variables correctly.
VBA Code:
Sub RedB()
Dim bnum As Variant: Dim bmod As Variant
bnum = Sheets(1).Range("AA2").Value
bmod = "Red_B" & bnum
bmod.Show (False)
Exit Sub
End Sub
VBA Code:
If Sheets(1).Range("AA2").Value = 1 Then
Red_B1.Show (False)
ElseIf Sheets(1).Range("AA2").Value = 2 Then
Red_B2.Show (False)
ElseIf Sheets(1).Range("AA2").Value = 3 Then
Red_B3.Show (False)
ElseIf Sheets(1).Range("AA2").Value = 4 Then
Red_B4.Show (False)
End If
Any help in properly correcting this code would be much appreciated!