Run - Time error

  • Thread starter Thread starter Patrick Simonds
  • Start date Start date
P

Patrick Simonds

The code below does what I need it to do (displays the current date in a
textbox when userform1 opens) but when I close the Userform I get the
following error:

Run-time error '91':
object variable or with block variable not set


Private Sub UserForm_Initialize()
With UserForm1
.DateTextBox.Text = Format(Date, "dddd dd-mmm-yy")
.Show
End With
End Sub
 
Simply remove the Show statement. You must already be
using the Show statement in a separate module to call the
UF. When it initializes, your code tells it to show itself
again even though it is already called. This causes the
error; however, you must close the UF before the error
message can display because the UF has priority.

Regards,
Greg
 
I did as you suggested, now my UserForm will not display. I have an
auto_open macro which is suppose to open the UserForm when the workbook
opens (Load UserForm1)
 
I didn't think you were using the Load statement. This, as
you're aware, loads but does not show the UF and lets you
manipulate it in the Initialize event.

My assumption is that the Show statement both loads and
shows a UF. Therefore, the Show statement creates a
redundacy (since you have already loaded it) which causes
the error. In the Auto_Open code, you can simply state
UserForm1.Show and forget the Load statement. Also, remove
the Show statement in the Initialize code. This works.

Regards,
Greg
 

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

Back
Top