Using a variable to open a userform

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.

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
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:
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!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi,
try using the CallByName Function & see if does what you want

Rich (BB code):
Sub RedB()
    Dim bnum As Variant, bmod As Variant
    Dim Form As Object
    
    bnum = Sheets(1).Range("AA2").Value
    bmod = "Red_B" & bnum

    Set Form = CallByName(UserForms, "Add", VbMethod, bmod)
    Form.Show

End Sub

You can read more in the VBA Helpfile: CallByName function (Visual Basic for Applications)
 
Upvote 0
Solution
Hi,
try using the CallByName Function & see if does what you want

Rich (BB code):
Sub RedB()
    Dim bnum As Variant, bmod As Variant
    Dim Form As Object
   
    bnum = Sheets(1).Range("AA2").Value
    bmod = "Red_B" & bnum

    Set Form = CallByName(UserForms, "Add", VbMethod, bmod)
    Form.Show

End Sub

You can read more in the VBA Helpfile: CallByName function (Visual Basic for Applications)
Awesome! That was what I needed. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

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