how to create a new code to achieve the goal, a macro save in A workbook, wanna select a new workbook to run this macro
时间: 2024-02-27 07:51:39 浏览: 99
To create a new code to achieve the goal of selecting a new workbook to run a macro saved in another workbook, you can use the following VBA code:
```
Sub RunMacroInAnotherWorkbook()
Dim wb As Workbook
Dim macroName As String
' Prompt the user to select the workbook containing the macro
Set wb = Application.GetOpenFilename("Excel Workbook (*.xlsx), *.xlsx")
' Prompt the user to enter the name of the macro to run
macroName = InputBox("Enter the name of the macro to run:")
' Check if the workbook was selected and the macro name was entered
If Not wb Is Nothing And macroName <> "" Then
' Open the selected workbook
Workbooks.Open wb
' Call the macro in the selected workbook
Application.Run "'" & wb.Name & "'!" & macroName
' Close the selected workbook without saving
wb.Close SaveChanges:=False
End If
End Sub
```
This code prompts the user to select a workbook containing the macro, then prompts for the name of the macro to run. It then opens the selected workbook, runs the macro, and closes the workbook without saving any changes. Note that this code assumes the macro is saved in a module in the selected workbook.
阅读全文
相关推荐











