Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

excel - How to run another workbook's macro?

I am writing some code in workbook1 to open and run macro in workbook2. Workbook_Name is the name of workbook2 with ".xlsm". I applied the following code in workbook1, hoping to run the macro "Sheet8.Compute" in workbook2. But its not working. Kindly assist.

ActiveSheet.Application.run "'" & Workbook_Name & "'!Sheet8.Compute"
question from:https://stackoverflow.com/questions/65841895/how-to-run-another-workbooks-macro

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Problem is that your Sub Compute is stored in the worksheet code of a Sheet (at least your line of code suggest that). In that case, you need to call it without the sheet name:

 Application.run "'" & Workbook_Name & "'!Compute"

However, it is good practice to put code other than event routines into a module. In that case, you can call the routine with or without the module name. Assuming module name Module1, you can use both:

 Application.run "'" & Workbook_Name & "'!Compute"
 Application.run "'" & Workbook_Name & "'!Module1.Compute"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...