Can't call a sub in a UserForm using Application.Run

M

Mike

I'm having problems calling Public Sub XXX that is in Userform1.

Tried

Application.Run "Userform1.XXX"

but will not work. Works OK if XXX is in a Standard Module,

Thanks for any help.
 
J

joel

Is the userform loaded? You aren't suppose to call user form routin
dirrectly. If yo have common code move the macro from the userform to
module and then call the routine.. You can call the common code from
userform, right!
 
M

Mike

It appears that Application.Run can be in a Standard Module, a Class Module
or a Userform module and it cannot call a sub in a Userform Module. Thanks.
 
M

Mike

The reason I have to use Application run is because I need the XXX to be a
variable, like

dim zzz as string

zzz= "Userform1.xxx"

Application.Run zzz

The Call userform1.xxx does work but "Call" can't take a variable.

Thanks Peter
 
C

Chip Pearson

The Call userform1.xxx does work but "Call" can't take a variable.

Use CallByName. E.g., in a regular code module


Sub AAA()
Dim FF As UserForm1
Dim S As String
S = "HelloWorld"
Set FF = New UserForm1
CallByName FF, S, VbMethod
End Sub

Here, HelloWorld is a method defined in UserForm1's code module. You
need to create an instance of UserForm1 but you need not Show the form
to make it visible.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com




The reason I have to use Application run is because I need the XXX to be a
variable, like

dim zzz as string

zzz= "Userform1.xxx"

Application.Run zzz

The Call userform1.xxx does work but "Call" can't take a variable.

Thanks Peter
 
M

Mike

Thanks Chip. That's exactly what I need. Works great!!


Chip Pearson said:
The Call userform1.xxx does work but "Call" can't take a variable.

Use CallByName. E.g., in a regular code module


Sub AAA()
Dim FF As UserForm1
Dim S As String
S = "HelloWorld"
Set FF = New UserForm1
CallByName FF, S, VbMethod
End Sub

Here, HelloWorld is a method defined in UserForm1's code module. You
need to create an instance of UserForm1 but you need not Show the form
to make it visible.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
 
Top