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.
 
When exactly are you trying to show UserForm2?
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
^^ nothing in the above code has anything (that I can see) to do with UserForm2. :confused:
See my previous post - how is UserForm2 being loaded if not in the same DoubleClick Event as UserForm1?
I think you are right. I may have to put UserForm2 in the same event (BeforeDoubleClick) as UserForm1.

When I did that, Excel did not crash. If I have UserForm1 call main and main call UserForm2, Excel crashes. But Why?

========= this code works fine ==========
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
UserForm1.Show
UserForm2.Show
End Sub

====the following code causes Excel to crash =================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
UserForm1.Show
End Sub
======== in UserForm1 ========
Private Sub CommandButton1_Click()
voucherDate = Format(TextBox1.Text, "dd/mm/yyyy")

If ...... Then
MsgBox ("You must make a selection.")

Else

Call voucher
Unload Me

End If

End Sub
========== in voucher() =========
......
......
UserForm2.Show '<- causes Excel to crash

======= The End ================
 
Upvote 0
When exactly are you trying to show UserForm2?
UserForm2 has one label "Enter more voucher?" and two buttons - Yes, No.

If user clicks No, then ,

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

I have not yet coded the Yes button because I want to solve the Excel crashing problem first. I just put "unload me" for now.
 
Upvote 0
Why is UserForm2.Show in the sub voucher?

Why not have it after the call to the sub in UserForm1?

Or use a message box
Code:
Dim resp As Variant
 
' call 
Call voucher()
 
' on return from voucher() sub ask the user if they want to continue
 
resp = MsgBox ("Do you wish to continue?", vbYesNo, "Continue")
 
Select Case resp
 
    Case vbNo
 
         Unload Me ' user doesn't wish to continue so unload UserForm1
 
    Case vbYes
 
         ' user does wish to continue so don't unload UserForm1
         ' but 'reset' it if required
 
End Select
 
Upvote 0
It appeared from your initial posts that userform2 was not getting capitalised to Userform2 in which case I still think you have something else called userform2.
Sorry! I may have been sloppy in my first posting. When I typed userform2.show in Excel, it did automatically capitalized the code words to UserForm2.Show.
 
Upvote 0
There's something out of whack but I really don't know what it is. I can create two forms, and put a button in one that calls a sub that opens the other. No problems. The mere fact of doing this should not be the cause of your crashes.
 
Upvote 0
There's something out of whack but I really don't know what it is. I can create two forms, and put a button in one that calls a sub that opens the other. No problems. The mere fact of doing this should not be the cause of your crashes.
I don't know what happened but it works now, with no apparent reason.

When trying to solve the problem, I even went to the extreme of exporting every form and the module, then, importing them to a blank sheet, and running the script. It still crashed. But this morning, it mysteriously works. The problem should not have been there. I don't know why it was there and why it went away.

I'm sorry I have wasted everybody's valuable time. Thank you for your kind help.
 
Upvote 0
Have you used the VBEditor Find feature to search the entire project for "userform2" ?

Another possibility might be that some control in Userform2 is causing the error, but the editor is breaking in the .Show statement. Is the VBEditor Preferences > General > Break in Class Module set?

Is there anything in the UF2 code that refers to a range? That reference might be effected if the UF is shown from a sheet module vs. normal module, causing the Object not Found error (breaking at the .Show statement).
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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