VBA - Using a variable to open a form

cmhoz

Active Member
Joined
Aug 20, 2006
Messages
268
Hi All -

I am building a system that essentially walks the user through a series of flow chart questions.

eg: Was the batch number found?

If NO then
If YES then

Depending on the answer, I want to open a certain form when they click next. I'm trying to do this by assigning the name of the form to a variable (via a lookup on a worksheet).

eg: nextform = "frmNext"

But when I then say.... nextform.show, it says I need an object. Clearly, this is because I haven't defined 'nextform' as being a form, but I can't find a way to do this??

Help?!?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi ,

As long as you have a form called 'frmNext' (check and create/change if necessary from the Visual Basic Editor) the following will do the trick:

Code:
Sub Macro2()

    Dim objNextForm As Object
    
    Set nextform = frmNext
    
    nextform.Show

End Sub

HTH

Robert
 
Upvote 0
This is what I have at the moment...

________________________________________
Private Sub btnNext_Click()

Dim nextform As Object

mylookup = Me.lblBatchOutcome.Caption
formname = WorksheetFunction.VLookup(mylookup, Range("findnextform"), 3, False)

Set nextform = formname
nextform.Show

End Sub
________________________________________

It works fine if I type the name of the form into "Set nextform = __"

However, when I try to use 'formname' here, I get a type mismatch.

I need to be able to use the variable and not the form name itself because the form I want to open will be determined by what is in the caption on the current form.
 
Upvote 0
Hi,

It's possible - see the code below:

Rich (BB code):

' Get UserForm object defined by its string name
Function Form(Name As String) As Object
  Set Form = CallByName(UserForms, "Add", VbMethod, Name)
End Function

Sub Test()
  Dim strFormName As String
  strFormName = "UserForm1"  ' <-- replace by your lookup code instead
  Form(strFormName).Show
End Sub

Regards,
Vladimir
 
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