Open Macros from another workbook

Analytic

New Member
Joined
Oct 31, 2015
Messages
16
Office Version
  1. 2019
Platform
  1. Windows
Hi,

Here is a code:

Code:
[COLOR=#333333]Private Sub CommandButton1_Click()


Dim varNames1 As Variant
Dim varNames2 As Variant


Dim lngArr As Long
Dim wb As Workbook


varNames1 = Array("FIRST\GRAND EVALUATIONS.xlsm")
For lngArr = LBound(varNames1) To UBound(varNames1)
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & varNames1(lngArr))
  Application.Run "'GRAND EVALUATIONS.xlsm'!Macro1"
  wb.Close True
  Set wb = Nothing
Next lngArr




varNames2 = Array("FIRST - Copy\GRAND EVALUATIONS.xlsm")
For lngArr = LBound(varNames2) To UBound(varNames2)
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & varNames2(lngArr))
  Application.Run "'GRAND EVALUATIONS.xlsm'!Macro1"
  wb.Close True
  Set wb = Nothing
Next lngArr


ActiveWorkbook.Save
ActiveWorkbook.Close


End Sub



[/COLOR]

Firstly the above code is supposed to open Macro1 of

Code:
[COLOR=#333333]varNames1 = Array("FIRST\GRAND EVALUATIONS.xlsm")
[/COLOR][COLOR=#333333]For lngArr = LBound(varNames1) To UBound(varNames1)[/COLOR]
[COLOR=#333333]  Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & varNames1(lngArr))[/COLOR]
[COLOR=#333333]  Application.Run "'GRAND EVALUATIONS.xlsm'!Macro1"[/COLOR]
[COLOR=#333333]  wb.Close True[/COLOR]
[COLOR=#333333]  Set wb = Nothing[/COLOR]
[COLOR=#333333]Next lngArr[/COLOR]


and it does that.

Secondly it was also expected to open Macro1 of

Code:
[COLOR=#333333]varNames2 = Array("FIRST - Copy\GRAND EVALUATIONS.xlsm")
[/COLOR][COLOR=#333333]For lngArr = LBound(varNames2) To UBound(varNames2)[/COLOR]
[COLOR=#333333]  Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & varNames2(lngArr))[/COLOR]
[COLOR=#333333]  Application.Run "'GRAND EVALUATIONS.xlsm'!Macro1"[/COLOR]
[COLOR=#333333]  wb.Close True[/COLOR]
[COLOR=#333333]  Set wb = Nothing[/COLOR]
[COLOR=#333333]Next lngArr

[/COLOR]

but it doesn't even open the file varNames2 = Array("FIRST - Copy\GRAND EVALUATIONS.xlsm").

Please provide me with some solutions to solve this problem.

Thank you
:confused:
 
Last edited:
The purpose is to calculate new values. and yes Results.xlsx does close.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Remove these two lines (red) from Macro1

Code:
Sub Macro1()
Dim varNames1 As Variant
Dim lngArr As Long
Dim wb As Workbook

varNames1 = Array("CAL1\RESULTS.xlsx")
For lngArr = LBound(varNames1) To UBound(varNames1)
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & varNames1(lngArr))
  wb.Close True
  Set wb = Nothing
Next lngArr

[COLOR=#ff0000]    ActiveWorkbook.Save
    ActiveWorkbook.Close[/COLOR]
    
End Sub

They are closing the ActiveWorkbook which at that point is GRAND EVALUATIONS.xlsm. Then back in your main workbook ENTRY BOARD.xlsm, the macro will try to close GRAND EVALUATIONS.xlsm again with wb.Close. So you have two different macros that are closing the same workbook.
 
Upvote 0
Remove these two lines (red) from Macro1

Code:
Sub Macro1()
Dim varNames1 As Variant
Dim lngArr As Long
Dim wb As Workbook

varNames1 = Array("CAL1\RESULTS.xlsx")
For lngArr = LBound(varNames1) To UBound(varNames1)
  Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & varNames1(lngArr))
  wb.Close True
  Set wb = Nothing
Next lngArr

[COLOR=#ff0000]    ActiveWorkbook.Save
    ActiveWorkbook.Close[/COLOR]
    
End Sub

They are closing the ActiveWorkbook which at that point is GRAND EVALUATIONS.xlsm. Then back in your main workbook ENTRY BOARD.xlsm, the macro will try to close GRAND EVALUATIONS.xlsm again with wb.Close. So you have two different macros that are closing the same workbook.

Thanks a lot AlphaFrog . That solved the problem. I do appreciate your great help.
Cheers
 
Upvote 0

Forum statistics

Threads
1,216,179
Messages
6,129,338
Members
449,504
Latest member
Alan the procrastinator

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