Run macro from an active workbook on another workbook

30percent

Board Regular
Joined
May 5, 2011
Messages
116
Office Version
  1. 2016
Platform
  1. Windows
Hi I have the following code. I'm trying to open a workbook (e.g. WorkWithExFile2) from current active workbook (e.g. WorkWithExFile) and run a procedure/macro that I have in active workbook (WorkWithExFile) on the workbook that I brought up (WorkWithExFile2) which was then saved as (test.xlsm)

I'm not sure this is the way to do it. I just search online and trying to mess with the code but I got an error message saying that: "Compile error: Expected Function or variable"

Code:
Sub Open_External_Workbook()


Dim open_Workbook As Workbook
Dim wb As Workbook


Workbooks.Open Filename:="C:\Users\Desktop\WorkWithExFile2.xlsm"


Set open_Workbook = Application.Workbooks.Open _
("C:\Users\Desktop\WorkWithExFile2.xlsm")


open_Workbook.SaveAs ("C:\Users\Desktop\test.xlsm")


Set wb = Workbooks.Open("C:\Users\Desktop\test.xlsm")


Application.Run wb.Name & "!" & proc_test


End Sub


Sub proc_test()


Worksheets("Sheet1").Range("A1").Value = "Ran proc_test"




End Sub

Wonder if someone could point out the error or the correct way to do it?

Thank you!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try to place breakpoints at each line please and watch which statement throws the error exactly.
 
Last edited by a moderator:
Upvote 0
Your path (C:\Users\Desktop) should be something like "C:\Users\Keith\Desktop '<---- Change Keith to your name.

If you want to find out the path to your desktop, try this code from snb.

Code:
Sub snb_Get_Desktop_Path()
    MsgBox CreateObject("wscript.shell").specialfolders(4)
End Sub
 
Upvote 0
If proc_test is in WorkWithExFile try
Code:
Sub Open_External_Workbook()


Dim open_Workbook As Workbook
Dim wb As Workbook

Set open_Workbook = Workbooks.Open("C:\Users\Desktop\WorkWithExFile2.xlsm")


open_Workbook.SaveAs ("C:\Users\Desktop\test.xlsm")


Set wb = Workbooks.Open("C:\Users\Desktop\test.xlsm")


Call proc_test


End Sub
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top