PDA

View Full Version : [SOLVED:] Running a Macro on another worksheet



simora
02-14-2025, 11:59 PM
I am trying to run a Macro on a different WorkBook.

When I "HARD CODE" the name of the WorkBook into the Macro it works, but how can I get it to work when I assign the name of the WorkBook to a variable.


The name of the workbook contains spaces.
This works: Application.Run ("'Search Database 2025 test.xls'!RedFont")

This is the bit of code that's creating the problem when I try to use it with a variable



ol2 = ActiveWorkbook.Name
Workbooks(ol2).Application.Run ("!RedFont")

jindon
02-15-2025, 03:18 AM
try


ol2 = CreateObject("Scripting.FileSystemObject").GetBaseName(ActiveWorkbook.Name)
Application.Run "'" & ol2 & "'!RedFont"

Paul_Hossler
02-15-2025, 04:35 AM
I would have thought that if the macro RedFont is in the ActiveWorkbook, that you'd just need to call it

simora
02-15-2025, 09:11 AM
Thanks jindon:

That works perfectly.

Just curious.

If I Dim ol2, exactly how would I declare it ?
As a String?

simora
02-15-2025, 09:19 AM
Paul_Hossler:

A couple of issues.

When the filename that you're calling from another workbook contains spaces, you come up with these issues if you're trying to use/call that workbook by referencing its Variable name from another Workbook.
Excel VBA APPARENTLY does NOT allow you to just do what you were suggesting.

That being said, I would like to see how others have gotten around this in addition to what jindon provided, which turned out to be a workable solution.

Thanks for your input.

simora
02-15-2025, 10:00 AM
BTW:

When calling A Macro on another workbook, If you create a function like:



Function GetWorkbookName() As String

GetWorkbookName = ActiveWorkbook.Name
End Function

Then you can use:

Application.Run "'" & GetWorkbookName & "'!RedFont"



To get the same result as:



ol2 = CreateObject("Scripting.FileSystemObject").GetBaseName(ActiveWorkbook.Name)
Application.Run "'" & ol2 & "'!RedFont

Paul_Hossler
02-15-2025, 01:35 PM
Paul_Hossler:

A couple of issues.

When the filename that you're calling from another workbook contains spaces, you come up with these issues if you're trying to use/call that workbook by referencing its Variable name from another Workbook.
Excel VBA APPARENTLY does NOT allow you to just do what you were suggesting.

That being said, I would like to see how others have gotten around this in addition to what jindon provided, which turned out to be a workable solution.

Thanks for your input.


I know that. I was just saying that this ...



ol2 = ActiveWorkbook.Name
Workbooks(ol2).Application.Run ("!RedFont")

... seems to be running a macro IN the ActiveWorkbook

Running macro ON the Activeworkbook that in a non-Activeworkbook is different

jindon22
02-15-2025, 07:09 PM
Thanks jindon:

That works perfectly.

Just curious.

If I Dim ol2, exactly how would I declare it ?
As a String?
You are welcome and thanks for the feedback.
ol2 should be a String type variable.

NOTE: I can still log in under jindon, but since I failed to update email address, I can only view the forum, so I created a new account under jindon22.

simora
02-16-2025, 03:37 PM
Paul_Hossler

I see your point.

What transpired before that is that the user had selected the other workbook, so the other remote workbook was selected and therefore became the Active Workbook.

Hope that makes sense. :yes

simora
02-16-2025, 03:40 PM
jindon22 (http://www.vbaexpress.com/forum/member.php?89449-jindon22)

Just curious:

Are we supposed to update our forum email address ?

simora
02-16-2025, 04:17 PM
jindon22 (http://www.vbaexpress.com/forum/member.php?89449-jindon22)

Just curious:

Are we supposed to update our forum email address ?

Aussiebear
02-16-2025, 06:13 PM
Simora, Jindon's issue is simply an administration one that I've since fixed for him.

Paul_Hossler
02-16-2025, 07:30 PM
Paul_Hossler
What transpired before that is that the user had selected the other workbook, so the other remote workbook was selected and therefore became the Active Workbook.
Hope that makes sense. :yes


I sort of follow


From post #1 ..



Running a Macro on another worksheet (Assume you meant 'Workbook')


I am trying to run a Macro on a different WorkBook.



It wasn't clear to me (at least) which WB had the macro and which WB the macro was intended to operate on

Since this works, I assumed that the WB 'Search Database 2025 test.xls' contained the macro RedFont


This works: Application.Run ("'Search Database 2025 test.xls'!RedFont")


What I missed was some how activating the macro containing WB, either manually or in code


Workbooks("'Search Database 2025 test.xls'").Activate

which as you said then makes Search Database 2025 test.xls the ActiveWorkbook in



ol2 = ActiveWorkbook.Name
Workbooks(ol2).Application.Run ("!RedFont")

jindon
02-16-2025, 08:13 PM
Simora,

No, you don't need to update your email address unless you need it to.

I was trying to change the email since old email is not convenient for me, but I failed to do it right.

I really appreciate the kind and quick action taken by Aussiebear.

simora
03-10-2025, 11:40 PM
Thanks guys for all your assistance.
Seems that some of my posts went into the AlienScape because I don't see them.