PC Review


Reply
Thread Tools Rate Thread

Can't call a subroutine

 
 
donwb
Guest
Posts: n/a
 
      6th Aug 2008
Excel 2003

The following works within my code:-
Call CommandButton10_Click
where CB10 is one of many on a Userform
and CommandButton10_Click is a subroutine "on" the Userform.

The problem comes when I try to make the CommandButton10 part a variable.
My code will produce a value for the variable "MyCommandButton" which might
be CommandButton15 for example.
Then I try to make the call as follows:-
Call MyCommandButton(&"_Click")
a compile error "Expected Sub, Function or Property" results.

Is this just my bad syntax, or am I trying to do something which VBA doesn't
like?
donwb


 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      6th Aug 2008
I don't know of a facility in VBA that will allow you to execute the click
event by code. But I am not the ripest banana in the bunch.

"donwb" wrote:

> Excel 2003
>
> The following works within my code:-
> Call CommandButton10_Click
> where CB10 is one of many on a Userform
> and CommandButton10_Click is a subroutine "on" the Userform.
>
> The problem comes when I try to make the CommandButton10 part a variable.
> My code will produce a value for the variable "MyCommandButton" which might
> be CommandButton15 for example.
> Then I try to make the call as follows:-
> Call MyCommandButton(&"_Click")
> a compile error "Expected Sub, Function or Property" results.
>
> Is this just my bad syntax, or am I trying to do something which VBA doesn't
> like?
> donwb
>
>
>

 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      6th Aug 2008
Try at

Application.Run MyCommandButton & "_Click"

Tim

"donwb" <(E-Mail Removed)> wrote in message
news:e0Ru%(E-Mail Removed)...
> Excel 2003
>
> The following works within my code:-
> Call CommandButton10_Click
> where CB10 is one of many on a Userform
> and CommandButton10_Click is a subroutine "on" the Userform.
>
> The problem comes when I try to make the CommandButton10 part a variable.
> My code will produce a value for the variable "MyCommandButton" which
> might be CommandButton15 for example.
> Then I try to make the call as follows:-
> Call MyCommandButton(&"_Click")
> a compile error "Expected Sub, Function or Property" results.
>
> Is this just my bad syntax, or am I trying to do something which VBA
> doesn't like?
> donwb
>



 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      6th Aug 2008
the easiest way to overcome these problems is to insert a module and place
the required code within a sub in the module like this

Sub CommandButton1_Click_Routine()
MsgBox "this sub called from command button1"
End Sub

then in the event code you call the sub in the module like this

Private Sub CommandButton1_Click()
Call CommandButton1_Click_Routine
End Sub


You can then call the sub in the module from any other event routine and you
don't have to try to call the actual event.


--
Regards,

OssieMac


"Tim Williams" wrote:

> Try at
>
> Application.Run MyCommandButton & "_Click"
>
> Tim
>
> "donwb" <(E-Mail Removed)> wrote in message
> news:e0Ru%(E-Mail Removed)...
> > Excel 2003
> >
> > The following works within my code:-
> > Call CommandButton10_Click
> > where CB10 is one of many on a Userform
> > and CommandButton10_Click is a subroutine "on" the Userform.
> >
> > The problem comes when I try to make the CommandButton10 part a variable.
> > My code will produce a value for the variable "MyCommandButton" which
> > might be CommandButton15 for example.
> > Then I try to make the call as follows:-
> > Call MyCommandButton(&"_Click")
> > a compile error "Expected Sub, Function or Property" results.
> >
> > Is this just my bad syntax, or am I trying to do something which VBA
> > doesn't like?
> > donwb
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call subroutine problem rick Microsoft Excel Programming 1 13th Nov 2009 10:01 AM
Call Subroutine in Excel DogLover Microsoft Excel Programming 3 5th Nov 2009 10:31 AM
Call a subroutine using variable subroutine name dhstein Microsoft Excel Misc 3 26th Jul 2009 08:28 PM
How do I call a VBA subroutine from Javascript SteveR Microsoft Outlook VBA Programming 4 18th Nov 2008 08:18 PM
Call subroutine that's in another database Connie Microsoft Access External Data 4 20th Feb 2006 10:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:35 AM.