Do userforms need initialization? - < Object variable or With block variable not set>

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
Do userforms need special initialization?

I have two userforms, userform1 and userform2. Excel crashes when it comes to the line "userform2.show".

I checked both forms' properties in the debug window. Userform1.name was userform1. That is fine. However, when it comes to userform2.name, the debug window showed "< Object variable or With block variable not set>" and its type was integer, not string.

I have codes in userform1's initialization routine but not userform2's. That makes me wonder whether I need to initialize userform2.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
How and when are you showing the forms?

Did you actually debug past the Show part?

Generally when you get en error there is something to do with the initialization.

It's usually the initialization code but not always, there could be something else happening.
 
Upvote 0
ISTM that your second form's name may not be exactly what you think it is. When you try to access it, VBA assumes it's a new variable of type integer, then gets confused when you try to access its .Name property as if it were an object.

When you right-click on the form and go View Code, what's in the title bar of the code window?

Place this in the form's click event handler:-
Code:
MsgBox """" & Me.Name & """ (" & Len(Me.Name) & " chars)"
then run the form and click it. Any surprises in the message box?
 
Upvote 0
Or you might have declared a variable somewhere with the name UserForm2.

If you want to check the name of a userform look at it's Name property while in design mode.
 
Upvote 0
How and when are you showing the forms?

Did you actually debug past the Show part?

Generally when you get en error there is something to do with the initialization.

It's usually the initialization code but not always, there could be something else happening.
Thank you for your reply.

Here is the entire code in UserForm2. I can't image what would go wrong.

When I ran only the form, it worked fine.

======================================
Option Explicit

Private Sub NoButton_Click()
Application.DisplayAlerts = False
Application.Quit
End Sub

Private Sub YesButton_Click()
Unload Me
End Sub
 
Upvote 0
Then I'd agree that either the form name is not what you think, or you have a variable by the same name, or perhaps the form is in a different workbook.
 
Upvote 0
ISTM that your second form's name may not be exactly what you think it is. When you try to access it, VBA assumes it's a new variable of type integer, then gets confused when you try to access its .Name property as if it were an object.

When you right-click on the form and go View Code, what's in the title bar of the code window?

Place this in the form's click event handler:-
Code:
MsgBox """" & Me.Name & """ (" & Len(Me.Name) & " chars)"
then run the form and click it. Any surprises in the message box?
Thanks for the reply.

I tried the message box. There is no surprise. The name of the form is UserForm2, length 9 characters.
 
Upvote 0
Are you prepared to make the workbook available for inspection, either via a file-sharing site or via email?

The data can be removed.
 
Upvote 0
What about the code for the other form?
 
Upvote 0
OK. I'm beginning to see the problem, maybe.

I have UserForm1.Show in sheet1's BeforeDoubleClick event and UserForm2.Show in module "main". Excel always crashes when it executes UserForm2.Show.

If I put UserForm2.Show in sheet1's BeforeDoubleClick event, right after UserForm1.Show (so, that event contains only two lines, UserForm1.Show and UserForm2.Show), there is no problem.

Anyone knows why?
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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