Hello - i have the following vba code (as below) in a workbook called "MASTER" which SHOULD look at all the excel workbooks i have open - select each workbook in turn (lets say wkbk1, wkbk2 and wkbk3) and in the newly selected workbook apply "macro1" contained within the new book.
Then close the new book and move onto the next available open book etc etc.
However the code is finding each workbook eg "wkbk1" then finding the macro1 but instead of applying "macro1" to the book it is contained in eg wkbk1 - it is applying the macro to the workbook called "MASTER" and not to wkbk1 etc
it just doesnt seem to be selecting wkbk1 to apply the code to for some reason.
i cannot have the macro1 contained within "MASTER" workbook because each "macro1" with each workbook is bespoke to that book and need regular updating.
Could anyone help me in forcing the macro1 to run in the bok where it sits.
Thank you in advance.
Sub RunMacroInOpenWorkbooks()
Dim wb As Workbook
Dim ws As Worksheet
Dim strMacroName As String
Dim i As Long
' Set the name of the macro to run
strMacroName = "macro1"
' Loop through all open workbooks
For Each wb In Workbooks
' Check if the workbook is not the current workbook
If wb.Name <> ThisWorkbook.Name Then
' Run the specified macro in the workbook
On Error Resume Next
Activate.wb
Application.Run "'" & wb.Name & "'!" & strMacroName
On Error GoTo 0
' Close the workbook without saving changes
wb.Close SaveChanges:=True
End If
Next wb
End Sub
Then close the new book and move onto the next available open book etc etc.
However the code is finding each workbook eg "wkbk1" then finding the macro1 but instead of applying "macro1" to the book it is contained in eg wkbk1 - it is applying the macro to the workbook called "MASTER" and not to wkbk1 etc
it just doesnt seem to be selecting wkbk1 to apply the code to for some reason.
i cannot have the macro1 contained within "MASTER" workbook because each "macro1" with each workbook is bespoke to that book and need regular updating.
Could anyone help me in forcing the macro1 to run in the bok where it sits.
Thank you in advance.
Sub RunMacroInOpenWorkbooks()
Dim wb As Workbook
Dim ws As Worksheet
Dim strMacroName As String
Dim i As Long
' Set the name of the macro to run
strMacroName = "macro1"
' Loop through all open workbooks
For Each wb In Workbooks
' Check if the workbook is not the current workbook
If wb.Name <> ThisWorkbook.Name Then
' Run the specified macro in the workbook
On Error Resume Next
Activate.wb
Application.Run "'" & wb.Name & "'!" & strMacroName
On Error GoTo 0
' Close the workbook without saving changes
wb.Close SaveChanges:=True
End If
Next wb
End Sub