简体   繁体   中英

Run a macro in a specified worksheet from another workbook

Here's my problem: I have two workbooks, one is mine, say Projet.xlsm, and the other is not, say Query.xlsm. Query.xlsm is shared between me and a lots of people in my company so I can't modify it.

My project.xlsm aims to plot some datas extracted with Query.xlsm. My job is to make it an automated process.

How Query.xlsm work: on the first sheet there is some options to select; then we have just to click a button on the first sheet. Finally all the datas are extracted and appears in second sheet.

I've built a macro in project.xlsm wich open Query.xlsm and select the options I want. Now I'm tryi,g to "click the button" (sheet1 in Query.xlsm) from my macro in my project.xlsm. I've tried things like this: running excel macro from another workbook But I think I have to mention the name of my sheet somewhere.

Thanks in advance

Assuming you have the workbook open (which it sounds like you do), this worked for me even on a Private CommandButton Sub.

Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = True
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = vbClick

You may create a public procedure in Query.xlsm Workbook in first sheet. cmdClick refers to the name of activex command button. Below is sample code.

Public Sub cmdClick()
    CommandButton1_Click
End Sub

You can call this procedure from project.xlsm using below syntax.

Workbooks("testing.xlsm").Sheets("Sheet1").cmdClick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM