Run-time error '424': Object Required

D

Doug

I have 2 userforms. The first opens just fine, the second gives me the
Run-time error. This is the code I'm using. How is one form different than
the other?

Sub Workbook_Open()
ActiveSheet.Range("H15").Select
If Selection.Value = "" Then
UserForm1.Show
Else
UserForm2.Show
End If
 
C

Chip Pearson

The code you present looks OK. The problem lies within the code of the
Userform. In the VBA editor, go to the Tools menu, choose Options,
then the "General" tab. There, in the "Error Trapping" section, choose
"Break In Class Module". Save your workbook and re-open it. The code
should enter debug mode on the exact line of code that is causing the
problem. (If you don't have error trapping set to "Break In Class
Module", debugging will stop on the line that instantiates a class --
your code's Show methods -- rather than on the line of code within the
class that actually caused the error.)
ActiveSheet.Range("H15").Select

I wouldn't rely on that. When you save and close a workbook, it will
re-open to the sheet and cell that was active when the workbook was
saved. This may not be the sheet that you think it should be. Instead,
do something like

Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("H15").Select

You can't just use
Worksheets("Sheet1").Range("H15").Select
alone because if Sheet1 isn't active, the code will fail. You must
first Select a sheet before Selecting anything on that sheet.

Or, much better, don't select anything at all. Get rid of the Select
code and use:

If Worksheets("Sheet1").Range("H15").Value = "" Then
UserForm1.Show
Else
UserForm2.Show
End If

In this case, it doesn't matter what sheet happens to be active when
the workbook is opened -- it will always test H15 on Sheet1.


Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
D

Doug

Thanks for the info. The detailed explanation really helps an amature like me.
Thanks
Doug
 

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