1

I developed a macro in a specific worksheet (for example: worksheet 1), but I want to run this macro in another worksheet (worksheet 2) without having to open the source code (worksheet 1).

Is there any way to call it?

Regards,

9
  • Have you tried simply typing the name of the other code? If the name in sheet 1 is Sub Test, in sheet 2's code, type Test or Call Test(Variables). If it's a Private Sub, you can type Call Sheet.Test Oct 7, 2017 at 14:53
  • the sequence: 1- open the worksheet 1; 2- access the macro tab and see/use the codes of other sheet 2 without open it. Oct 7, 2017 at 15:03
  • By Macro Tab, do you mean the same thing as Alt+F8? If that is what you are talking about, go to your modules and make sure they are not showing Private Sub, you can change to Public Sub or just Sub. Oct 7, 2017 at 15:13
  • they are not private sub, but to see macro do I have necessary to open the source code? is there any way to select a folder to read all macros in different sheets? Oct 7, 2017 at 15:32
  • How do you want to "See" the macros? By "See", do you just mean "Run"? Are you wanting to "Run" all macros at the same time? If you want to see a list of them that you can run individually, you can create a UserForm and add buttons that will call each macro if that's what you want. I am just not really understanding what exactly you are asking for. Oct 7, 2017 at 15:37

2 Answers 2

1

You cannot run VBA code located in a Closed workbook. However, you can still run code from another workbook that is currently open. You can use VBA code in your current workbook that can automatically open the other workbook, run the code, then close the workbook.

So to open a workbook, Workbooks.Open. To run the macro in that workbook, you will need to use Application.Run "FileName.xlsm!YourMacroNameHere".

There is one thing you will need to be aware of when running code from another workbook. If the code makes references to that workbook only, then your code will not make changes in the new workbook.

Example:

If the code you are using says things such as Set Ws = ThisWorkbook.Worksheets(), then anything that references Ws will not update the book that called it (hence, it will not reference the book with the code Application.Run)

2
  • Oh Thanks! that is what I wanted, really apprecciate it! Oct 7, 2017 at 16:02
  • Hello, what can i do to avoid ThisWorkbook.Worksheets() ? I have it in my macro, how can I set it to the right workbook ?
    – Benoît
    Oct 29, 2021 at 13:44
0

Although the approved answer is correct, there is also such a thing as the "Personal Macro Workbook". You can find this workbook when trying to record a new macro, in the "store macro in" dialog. This workbook is available across all your workbooks and does not need to be opened for your macros to run.

Personal macro workbook screenshot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.