Run 2 Macros in a Second Workbook

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
974
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi everyone,

I have a question on how to run 2 macros in a second workbook
I have Workbook 1 open and I want to update a total of 42 worksbooks (workbook 2, 3, etc. using the calculations from Workbook 1
Can I use the workbook2 name in cell A2?. Example: workbook2.xlsm

This is the code below and I know it's likely incorrect:

Sub RunMacrosIn2Workbooks()

Sname1 As String

'Cell A1 contains the name of the Workbook 2 to open
Sname1 = Sheets("Sheet1").Range("A1").Value


'Open workbook 2
Workbooks.Open (Sname1)

Sheets("Dashboard").Select

'run 1st macro in workbook 2
Application.Run (" 'Workbook2.xlsm' !ClearPreviousResults1")

'run 2nd macro in workbook 2
Application.Run (" 'Workbook2.xlsm' !CopyData1")

'Save and Close Workbook 2
ActiveWorkbook.Close True


Windows("Workbook1.xlsm").Activate
Sheets("sheet1").Select
Range("B1").Select

End Sub

My question is, can I use the following 2 lines of code instead of the 2 lines of code above?

'run 1st macro in workbook 2
Application.Run (" 'Sname1' !ClearPreviousResults1")

'run 2nd macro in workbook 2
Application.Run (" 'Sname1' !CopyData1")

Thank you
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I was able to run the code using the following:

Sub RunMacrosInWorkbook2()

Dim aRng As String, Sname1 As String

'Cell A1 contains the name of the Workbook 2 to open
Sname1 = Sheets("SelectPick3Games").Range("Q30").Value


'Open Workbook 2
Workbooks.Open (Sname1)

Sheets("Dashboard").Select

'run 1st macro in workbook 2
Application.Run "NewJersey.xlsm!ClearPreviousResults1"

'run 2nd macro in workbook 2
Application.Run "NewJersey.xlsm!CopyStringsData1"

'Save and Close Workbook 2
ActiveWorkbook.Close True


Windows("Pick3Stats.xlsm").Activate
Sheets("SelectPick3Games").Select
Range("Q28").Select

End Sub

Since I need to calculate 41 other workbooks I tried using the following code without any success.
I keep getting 1004 run-time error using the 2 lines of code below
"run-time error 1004 The file cannot be opened because your virus scanner has detected a problem with the file"

Sname1 seems to keep the code from running or the line of code is incorrect

'run 1st macro in workbook 2
Application.Run "Sname1!ClearPreviousResults1"

'run 2nd macro in workbook 2
Application.Run "Sname1!CopyStringsData1"

Thank you
 
Upvote 0
This was the fix of the 2 lines of code below:

'run 1st macro in workbook 2
Application.Run Sname1 & "!ClearPreviousResults1"

'run 2nd macro in workbook 2
Application.Run Sname1 & "!CopyStringsData1"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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