Using same variable to refer to multiple forms

RobertSF

Board Regular
Joined
Jun 20, 2017
Messages
104
I have a form with a button that, when clicked and depending on a separate value, will launch one of two possible forms.

Code:
dim frm as UserForm

'later
If theValue = "edit" then
    Set frm = frmEdit
ElseIf theValue = "delete" then
    Set frm = frmDelete
End If

frm.Show 'Run time error 438: Object doesn't support this property or method

Indeed, I can access no properties of frm. Even ?frm.name gives an error. Both frmEdit and frmDelete are valid forms.

Any idea if this is possible?
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try
Code:
Dim frm As [COLOR=#ff0000]Object[/COLOR]
 
Upvote 0
Perhaps neither condition is true....

try:

If LCase(theValue) = "edit" then
set frm = frmEdit

'and

ElseIf LCase(theValue) = "delete" then
set frm = frmDelete
'also, add an else condition....

Else
msgbox "frm not set"
exit sub
end if
 
Upvote 0
Try
Code:
Dim frm As [COLOR=#ff0000]Object[/COLOR]
This has always amazed me. The pop up selection menu lists UserForm as an option, but it errors if used. I think MS should fix that.
 
Upvote 0
Must admit I've never figured out when you would dim as Userform, That's assuming that there would be a scenario when you'd use it.
I either Dim as object, or Dim as Userform1
 
Upvote 0
Must admit I've never figured out when you would dim as Userform, That's assuming that there would be a scenario when you'd use it.
I either Dim as object, or Dim as Userform1

I don't believe I have ever declared a variable for a userform. I just refer to the names in most cases, but this code has been written to use a variable so it should be declared.
 
Upvote 0
Thanks everyone for the suggestion to declare the variable an Object instead of a UserForm.

I'll try it.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
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