VBA Macro w/in a Macro ?

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All;

I have a quick ? with a Macro w/in a Macro recorded code.

Let me try to explain it, here goes...


Below is a Macro that I recorded that containes several written Macros, as you can see.


Sub Macro10()
'
' Macro10 Macro
'
' Keyboard Shortcut: Ctrl+Shift+Y
'

Application.Run "'Excel-File-Name-1.xlsm'!Macro12"
Application.Run "'Excel-File-Name-1.xlsm'!Macro1"
Application.Run "'Excel-File-Name-1.xlsm'!Macro8"
Application.Run "'Excel-File-Name-1.xlsm'!Macro9"
Application.Run "'Excel-File-Name-1.xlsm'!Macro4"
Application.Run "'Excel-File-Name-1.xlsm'!Macro13"
Application.Run "'Excel-File-Name-1.xlsm'!Macro5"
Application.Run "'Excel-File-Name-1.xlsm'!Macro6"

End Sub


My question is; when you rename the file(ie 'EXCEL-File-Name-2.xlsm'), it will not execute the command macro because the file name has since been changed.

Is there anyway around this, in terms of having a command macro, which containes a string of written macros for its a file name, where it is then copied and renamed to a different file name, where this command macro can still function w/o having to go into the VBA in order to change its name being; Application.Run "'Excel-File-Name-2.xlsm'!Macro##" ?


Hope my point is clear:confused:.

Thanks in advance.

R/

-Pin
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You can set the filename as a string. So then instead of changing the code in 20 different places you can simply change the one line.

For Example:

Code:
[COLOR=#0000ff]Dim [/COLOR]FileName[COLOR=#0000ff] As String[/COLOR]

FileName = "Your FileName"

Application.Run "'" & FileName & "'!Macro6"
Application.Run "'" & FileName & "'!Macro7"
Application.Run "'" & FileName & "'!Macro8"

or maybe put the macros in your Personal Workbook then there is no need to Qualify them like that...
 
Upvote 0
Thanks very much, I'm going to try it now
R/
-Pin:)
 
Upvote 0
Why can't you just Call the macros in your main macro?

i.e.

Code:
Sub Macro10()
 '
 ' Macro10 Macro
 '
 ' Keyboard Shortcut: Ctrl+Shift+Y
 '

 Call Macro12
Call Macro1
Call Macro8
Call Macro9
 Call Macro4
Call Macro13
Call Macro5
Call Macro6

 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,521
Members
449,088
Latest member
RandomExceller01

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