close UserForm

M

maximilianchu

Received a runtime 91 error "Object variable or With block variable not
set" error when closing an user form. Excel debug go right to 'Load
UserForm1" statement. All code run smoothly up to this point.

Help!
 
G

Guest

Sounds like you have an error in your code where you try to use an object
that is not set to a reference. Probably in some code that fires when the
userform is closed.

for instances if you had

Private Sub Cmd_OK_Click()
rng.Value = Textbox1.Value
Unload me
End sub

if rng was not pointing to a range object, you would get such an error.
 
C

Chip Pearson

Without seeing the code in the UserForm_Terminate event, it is difficult to
determine the cause of the problem. In order to have the editor take you to
the offending line of code, in VBA go to the Tools menu, choose Options,
then the "General" tab. There, select "Break In Class Module" instead of
"Break On Unhandled Errors".


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com
(email address is on the web site)
 
G

Guest

I'm having the same problem unloading a userform...thought until last night
that error 91 was caused by trying to show a highlighted default in the
listbox because without the line .ControlSource = "Sheet1!AA19" everything
worked fine. Last night I discovered the default and everything else worked
without the Unload Me command except that the userform won't unload (Duh!) I
tried Unload CropMonth, which is the name of the userform but I get the same
error. It's a userform with only a single selection listbox which I want to
default to the last selection when it initializes and close when the user
clicks a selection. Here's the code:

Private Sub ListBox1_Click()
Sheets("sheet1").Range("AA19").Value = ListBox1.Value
Unload Me '(everything works w/o this line except won't unload
form.)
End Sub

Private Sub UserForm_Initialize()
With ListBox1
.RowSource = ""
.AddItem "N/A"
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
.ControlSource = "sheet1!AA19" '(Everything works w/o this but no
default.)
End With
End Sub

I would also appreciate any help regarding this problem. Thanks, Katrina
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top