Select an Open workbook and apply a macro1 contained in the new book to the new book

glynn1969

Board Regular
Joined
Nov 24, 2018
Messages
88
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello!
VBA Code:
Sub RunMacroInOpenWorkbooks()
Dim wb As Workbook, strMacroName As String
strMacroName = "macro1"
    If Workbooks.Count >= 2 Then
        For Each wb In Workbooks
            If wb.Name <> ThisWorkbook.Name Then
                On Error Resume Next
                wb.Activate
                Application.Run "'" & wb.Name & "'!" & strMacroName
                On Error GoTo 0
                wb.Close SaveChanges:=True
            End If
        Next wb
        MsgBox "mission accomplished"
    Else
        MsgBox "no more open workbooks"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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