VBA Macro w/in a Macro ?

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
957
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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,461
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

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
957
Office Version
  1. 365
Platform
  1. Windows
Thanks very much, I'm going to try it now
R/
-Pin:)
 
Upvote 0

jtakw

Well-known Member
Joined
Jun 29, 2014
Messages
7,245
Office Version
  1. 2016
Platform
  1. Windows
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,191,415
Messages
5,986,428
Members
440,029
Latest member
GabSSSH

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
Top