View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default VBA code to send click command to a macro in a different Excelworkbook

Saved from a previous post (but very similar to your question about clicking a
commandbutton on a different worksheet in the SAME workbook).

(The original poster used the names of Harry and George for his/her
commandbuttons. You'll want to modify them to match your names. The button
Harry "clicked" the button George.)

You have at least a couple of choices.

This is the code I used under the Sheet2 module.
Option Explicit
Sub George_Click()
MsgBox "hi from George"
End Sub

Notice that the Private keyword is gone. That's so the "Call" can find the
procedure.

This is the code under the Sheet1 module:
Option Explicit
Private Sub Harry_Click()
Call Sheet2.George_Click
End Sub

=========
Another way:
Under Sheet2:
Option Explicit
Private Sub George_Click()
MsgBox "hi from George"
End Sub

Notice that the Private keyword is back.

And under Sheet1:
Option Explicit
Private Sub Harry_Click()
Application.Run "Sheet2.George_Click"
End Sub

(in both of these cases, that Sheet2 is the code name for the sheet--not the
name you see in excel on the sheet tab.)

=======
And one more way:
Under Sheet2:
Option Explicit
Private Sub George_Click()
MsgBox "hi from George"
End Sub

Under Sheet1:
Option Explicit
Private Sub Harry_Click()
Worksheets("Sheet2").OLEObjects("George").Object.V alue = True
End Sub

Will, Anikouchine wrote:

My problem is the same as Raj's. I wish to write code in VBA to, in effect, remotely click a command button on a different worksheet. I needd a bit more guidance than "see help for Application.Run". I don't even know where to find that.

Any help would be greatly appreciated.

Posted as a reply to:

Macro code for clicking on Command Button in another workbook

Hi,

I have a workbook with macros. The macros open another workbook with
macros created by another person with code protected. A sheet in that
workbook has a command button that is to be clicked to run the macros
in that workbook. Is there a programmatic way by which I can click the
command button from my workbook macro code?

Thanks in Advance for the Help.

Regards,
Raj

EggHeadCafe - Software Developer Portal of Choice
WCF Workflow Services Using External Data Exchange
http://www.eggheadcafe.com/tutorials...vices-usi.aspx


--

Dave Peterson