Sie sind auf Seite 1von 9

pdfcrowd.com open in browser PRO version Are you a developer?

Try out the HTML to PDF API


Related Content
Random Sort Data Macro

Applies To: Office | Office 2007 | Microsoft Office Excel | Office on Windows XP
Question

VBA for Excel : Dimension UserForm as Object
Hi,

Want my code to show my UserForm. Got this code:

Sub FormatCODSfile()
'
' FormatCODSfile Macro
'
If SheetExists("vesselReconciliation.jsp") Then
UserForm1.Show
Else
MsgBox ("No CODS report found. Please export Cargo Readiness report to excel before running
macro again.")
Had this question
Me Too 1
GonaloCouto asked on March 28, 2013
By using this site you agree to the use of cookies for analytics, personalized content and ads. Learn More
Sign in
Community
Office

February 17, 2014 Microsoft Community Maintenance
Announcement
The Microsoft Community website will undergo system maintenance on 19
February 2014 09:00 13:00 Pacific Standard Time (UTC/GMT -8 hours).
During the system maintenance, you will be able to find and read content, but
you will be unable to create new posts or comments. Thank you for your
patience.
Announcements:
1

Home Categories Participate Additional Support
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
End If
End Sub
This step should be as easy as:

UserForm1.Show

but not working. Any ideas why?

Tried also

Dim UF As Object
Set UF = UserForm1
UF.Show

But the reference to the UserForm1 is not usable by VBA not sure why...
Reply | Reply with quote | Report abuse | Subscribe to Updates
Answer
In reply to GonaloCouto post on March 28, 2013
Hi,

That code works fine and there's nothing I can see that would give a 'subscript out of range' error. If the
userform name was wrong you would get Runtime error 424 - Object required.


My best guess is that as the userform tries to load you have some code in the initialise event that looks
for something that isn't there such as a worksheet. That would give a subscript out of range error and the
Mike H.. replied on
Community Moderator
March 28, 2013
Found this helpf ul
Me Too 1
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
If this response answers your question then please mark as Answer.
Mike H
userform wouldn't load. Instead the code would fail on the line userform1.show.


Do you have any initialise code in the userform? Yes? Carefully check the code for spelling mistakes for
things like worksheet names.
Reply | Reply with quote | Report abuse
Answer
The problem isn't with the UserForm1.Show line of code. The problem lies in the code within that form's
code module. You need to change your error trapping settings to get to the error. In VBA, go to the Tools
menu and choose Options. There, go to the General tab and select "Break In Class Module" in the Error
Trapping group. Now, when you run the code, the debugger will take you to the actual line of code that is
causing the error.


If the error trapping setting is not "Break In Class Module" and an error occurs during the initialization of
a class (and a UserForm is just a special type of class), the debugger will stop at the line of code that
created the object from the class, which in your case is the UserForm1.Show line of code. When "Break
In Class Module" is in effect, the debugger will take you inside the class to the line of code that caused
the problem.

You should *always* have "Break In Class Module" selected. There is no reason not to.





Chip Pearson replied on
MVP
March 28, 2013
Found this helpf ul
Me Too 0
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Cordially,
Chip Pearson
Excel MVP 1998 - 2014
Pearson Sof tware Consulting, LLC
www.cpearson.com
Reply | Reply with quote | Report abuse
All Replies (5)
If this response answers your question then please mark as Answer.
Mike H
Hi,

I don't know what 'SheetExists' is but from the name it's a fair bet it's checking for the existence of that
worksheet. If it does exist then the userform should show.

If something as simple as userform1.show isn't working then are you sure that's the correct name of the
userform?


Try this. In your code module type Userform1.

When you type in the period you should get a dialog of the properties for the userform. Do you see it?

Post back again and tell us the exact error message you get.
Reply | Reply with quote | Report abuse
Mike H.. replied on
Community Moderator
March 28, 2013
Found this helpf ul
Me Too 0
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
In reply to Mike H.. post on March 28, 2013
Yeah, SheetExists is a function. Well spotted.

Sub FormatCODSfile()
'
' FormatCODSfile Macro
'
If SheetExists("vesselReconciliation.jsp") Then
UserForm1.Show
Else
MsgBox ("No CODS report found. Please export Cargo Readiness report to excel before running macro
again.")
End If
End Sub
Function SheetExists(ByVal strSheetName As String) As Boolean
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(strSheetName)
If Err = 0 Then
SheetExists = True
Else
SheetExists = False
End If
End Function

The form is really called UserForm1 although the caption is different.
The exact error message is as follows:

After I get a Run-time error '9':
Subscript out of range

I try to debug and "UserForm1.Show" is highlighted, when the mouse cursor is over the highlight I get:

GonaloCouto replied on March 28, 2013
Found this helpf ul
Me Too 0
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
UserForm1 = <Object variable or With block variable not set>



I did not mention that the whole VBAProject (including Forms and Modules with macros to run) are stored in
a hidden file that opens every time excel is opened (personal.xlsb). Maybe that has something to do with it?

Thanks for your help.
Reply | Reply with quote | Report abuse
Answer
If this response answers your question then please mark as Answer.
Mike H
In reply to GonaloCouto post on March 28, 2013
Hi,

That code works fine and there's nothing I can see that would give a 'subscript out of range' error. If the
userform name was wrong you would get Runtime error 424 - Object required.


My best guess is that as the userform tries to load you have some code in the initialise event that looks for
something that isn't there such as a worksheet. That would give a subscript out of range error and the
userform wouldn't load. Instead the code would fail on the line userform1.show.


Do you have any initialise code in the userform? Yes? Carefully check the code for spelling mistakes for
things like worksheet names.
Mike H.. replied on
Community Moderator
March 28, 2013
Found this helpf ul
Me Too 1
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
Reply | Reply with quote | Report abuse
Answer
Cordially,
Chip Pearson
Excel MVP 1998 - 2014
Pearson Sof tware Consulting, LLC
www.cpearson.com
The problem isn't with the UserForm1.Show line of code. The problem lies in the code within that form's code
module. You need to change your error trapping settings to get to the error. In VBA, go to the Tools menu
and choose Options. There, go to the General tab and select "Break In Class Module" in the Error Trapping
group. Now, when you run the code, the debugger will take you to the actual line of code that is causing the
error.


If the error trapping setting is not "Break In Class Module" and an error occurs during the initialization of a
class (and a UserForm is just a special type of class), the debugger will stop at the line of code that created
the object from the class, which in your case is the UserForm1.Show line of code. When "Break In Class
Module" is in effect, the debugger will take you inside the class to the line of code that caused the problem.

You should *always* have "Break In Class Module" selected. There is no reason not to.





Reply | Reply with quote | Report abuse
Chip Pearson replied on
MVP
March 28, 2013
Found this helpf ul
Me Too 0
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
In reply to Chip Pearson post on March 28, 2013
The problem isn't with the UserForm1.Show line of code. The problem lies in the code within that form's
code module. You need to change your error trapping settings to get to the error. In VBA, go to the Tools
menu and choose Options. There, go to the General tab and select "Break In Class Module" in the Error
Trapping group. Now, when you run the code, the debugger will take you to the actual line of code that is
causing the error.


If the error trapping setting is not "Break In Class Module" and an error occurs during the initialization of a
class (and a UserForm is just a special type of class), the debugger will stop at the line of code that
created the object from the class, which in your case is the UserForm1.Show line of code. When "Break
In Class Module" is in effect, the debugger will take you inside the class to the line of code that caused
the problem.

You should *always* have "Break In Class Module" selected. There is no reason not to.






Thanks, I'll certainly mark this too as an answer. Just need some help with another thing maybe you can
help.

The comboboxes I have only work when testing them directly from VBA using the "Run" or F5. If I try prompt
the form in "live" setting (in this case, after pressing a set of keys a macro prompts the form to show) the
combboboxes are blank! Any idea why?

The source data (and also all modules and forms) are on a personal.xlsb file that is hidden and opens every
time excel is open.

Thanks for the assistance
GonaloCouto replied on March 28, 2013
Found this helpf ul
Me Too 0
pdfcrowd.com open in browser PRO version Are you a developer? Try out the HTML to PDF API
English
Microsoft Community Code of Conduct Get to Know the Microsoft Community
Trademarks Privacy & Cookies Terms of Use
2014 Microsoft
Reply | Reply with quote | Report abuse

Das könnte Ihnen auch gefallen