Consulting

Results 1 to 15 of 15

Thread: Running a Macro on another worksheet

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location

    Running a Macro on another worksheet

    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")

  2. #2
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    173
    Location
    try
        ol2 = CreateObject("Scripting.FileSystemObject").GetBaseName(ActiveWorkbook.Name)
        Application.Run "'" & ol2 & "'!RedFont"

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,818
    Location
    I would have thought that if the macro RedFont is in the ActiveWorkbook, that you'd just need to call it
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    Thanks jindon:

    That works perfectly.

    Just curious.

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

  5. #5
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    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.

  6. #6
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    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

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,818
    Location
    Quote Originally Posted by simora View Post
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    jindon22
    Guest
    Quote Originally Posted by simora View Post
    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.

  9. #9
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    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.

  10. #10
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    jindon22

    Just curious:

    Are we supposed to update our forum email address ?

  11. #11
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    jindon22

    Just curious:

    Are we supposed to update our forum email address ?

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,327
    Location
    Simora, Jindon's issue is simply an administration one that I've since fixed for him.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,818
    Location
    Quote Originally Posted by simora View Post
    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.

    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")
    Last edited by Paul_Hossler; 02-16-2025 at 07:34 PM. Reason: Futzing around for clarity
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  14. #14
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    173
    Location
    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.

  15. #15
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    402
    Location
    Thanks guys for all your assistance.
    Seems that some of my posts went into the AlienScape because I don't see them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •