Consulting

Results 1 to 13 of 13

Thread: Error while trying to run a macro using a selected workbook

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

    Error while trying to run a macro using a selected workbook

    I'm trying to run a macro using Application.Run from a workbook that is selected.
    I'm getting an error telling me that the macro is NOT Available.
    I'm using Attach = Application.GetOpenFilename to select a workbook so that I can run a macro on it.
    After I have selected the workbook, I've asssigned ol as the variable for it.
    I had tried using macroName but that didn't work either, so I hardCoded the Macro Name for simpicity.

    Need some help with my obviously wrong syntax.
    Dim macroString As String
    Dim ol As Workbook
    Dim macroName As String
    macroName = "MakeFinalVendors"       ' The name of the Sub procedure in the target workbook
    Set ol = ActiveWorkbook  
    ol.Activate
    ' Checking to make sure that I'm on the correct workbook
    MsgBox ActiveWorkbook.Name
    macroString = "'" & activeWorkbookName & "'!MakeFinalVendors'"
    ' Execute the macro
    Application.Run macroString   [  THIS IS GIVING ME  THE  ERROR ]

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    449
    Location
    Where is the code for GetOpenFilename?

    Why have variable macroName then not even use it?

    Where is variable activeWorkbookName declared and set?

    I tested code using GetOpenFilename. The active workbook is the one with posted procedure, not the one with MakeFinalVendors.

    This one-liner worked:
    Sub test()
        Application.Run "'" & Application.GetOpenFilename & "'!MakeFinalVendors'"
    End Sub
    Last edited by June7; 07-28-2025 at 09:39 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    You omitted a period between ActiveWorkbook and Name. This is the correct code:

    macroString = "'" & activeWorkbook.Name & "'!MakeFinalVendors'"
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  4. #4
    VBAX Regular DocAElstein's Avatar
    Joined
    Oct 2023
    Location
    Hof, Germany
    Posts
    13
    Location
    Hi
    I have an answer, but could not get it to post here. Strangly it posted OK in the test sub forum, so you can check it out in my last post there, ( Post #5 in my Thread there )
    ( I am denied if I try to give you the URL link to it )
    Alan

    Testing Area.JPG
    Test Post Answer.JPG
    Attached Files Attached Files
    Last edited by DocAElstein; 07-30-2025 at 02:08 PM.

  5. #5
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    449
    Location
    Alan, you did post in this thread, with embedded images and attachments - so exactly what issue did you encounter? Was it posting code within CODE tags? Odd that it was okay in other thread.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    VBAX Regular DocAElstein's Avatar
    Joined
    Oct 2023
    Location
    Hof, Germany
    Posts
    13
    Location
    Hello June7
    It took a long time for me to finally get this reply to you. The forum is almost certainly having issues currently…

    The error I experienced / experience the most was that
    Error 503

    Error 503.JPG


    I had a few others, well known to me***, such as the database one

    VBAExpress Database error.jpg




    In addition to that I was occasionally told that there were forbidden words in the post. (I did try rewording but that had no effect. Finally what I posted in the other test sub forum Thread was exactly what I had tried many times and tried the most in the main Thread. It did not post first time there in the test sub forum either, as I got the Error 503 etc. , a few times there as well.)


    Quote Originally Posted by June7 View Post
    Odd that it was okay in other thread.
    I did not seem to get that main error so often in the test sub forum, that is all. That may not mean much - ### It is very difficult to draw any conclusions to where the problems lie:
    Possibly this forum is having some issues similar to many forums since May of this year. I see that a few people here have mentioned these issues over the last few months. It seems to have its origins in the Forum being flooded with views from a new breed of Bots, most likely originating in the Peoples Republic of China.


    ***I have been fighting this problem hard for a few months now on and off over at Excel Fox
    I have a ton of experiments and results, and observations, documented currently in Threads at Excel Fox. I am just starting to get some possible conclusions and solutions to the problem. (We are using the same software as here, vBulletin Version 4.2.5 )

    ###It is very difficult to draw conclusions as to the exact cause of such problems, or to find the solution, as what is going on is not very constant or consistent at all. For example they can occasionally go away completely, and then suddenly they come back with great vengeance, but doing their dasterdly deeds in a slightly different way.

    Alan

    (P.S. it is likely that as a newish member I will also be denied posting URLs for a while. I am aware that this a typical anti spam feature. )
    Last edited by DocAElstein; Yesterday at 02:53 AM.

  7. #7
    VBAX Regular DocAElstein's Avatar
    Joined
    Oct 2023
    Location
    Hof, Germany
    Posts
    13
    Location
    Quote Originally Posted by Jan Karel Pieterse View Post
    You omitted a period between ActiveWorkbook and Name. This is the correct code:

    macroString = "'" & activeWorkbook.Name & "'!MakeFinalVendors'"
    Hi Jan,
    I think you may be missing a ' in your suggestion
    macroString = "'" & activeWorkbook.Name & "'!'MakeFinalVendors'"


    Alan

    Edit, P.S: simora also had that missing ' in the original Post

    Edit: Having just reached a post count of 10 I can now possibly pass a link to my solutiuon
    http://www.vbaexpress.com/forum/show...l=1#post426145
    https://www.excelfox.com/forum/showt...ll=1#post25100
    Last edited by DocAElstein; Yesterday at 04:11 AM. Reason: P.S: simora also had that missing ' in the original and links Post

  8. #8
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    449
    Location
    Oops, copy/paste error on my part - I copy/pasted OPs code for macro name part to replace my macro name. My corrected code without extra apostrophe:

    Application.Run "'" & Application.GetOpenFilename & "'!MakeFinalVendors"
    Apostrophes around macro name are not required but will work.
    Last edited by June7; Yesterday at 09:10 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    VBAX Regular DocAElstein's Avatar
    Joined
    Oct 2023
    Location
    Hof, Germany
    Posts
    13
    Location
    Quote Originally Posted by June7 View Post
    Oops, copy/paste error on my part - I copy/pasted OPs code for macro name part to replace my macro name. My corrected code without extra apostrophe:

    Application.Run "'" & Application.GetOpenFilename & "'!MakeFinalVendors"
    Apostrophes around macro name are not required but will work.
    Ahh, yes, I had not noticed that you also had that error.
    I learnt something as well. I never noticed that one could do away with the Apostrophes around the macro name.
    (Access to the forum seems better for me today)

  10. #10
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,891
    Location
    Quote Originally Posted by DocAElstein View Post
    Ahh, yes, I had not noticed that you also had that error.
    I learnt something as well. I never noticed that one could do away with the Apostrophes around the macro name.
    (Access to the forum seems better for me today)
    Actually I think the single quotes are around the workbook file name, not the macro name

    If the wb file name contrains spaces I'd bracket it with single quotes.

    Since a macro name cannot have spaces, it's not really a problem thee
    ---------------------------------------------------------------------------------------------------------------------

    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

  11. #11
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    449
    Location
    The apostrophes HAVE to be around filepath\name even if there are no spaces. They CAN be around macro name.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    VBAX Regular DocAElstein's Avatar
    Joined
    Oct 2023
    Location
    Hof, Germany
    Posts
    13
    Location
    Hi
    Yes that is the thing.
    I had prepared an answer to Paul_Hossler, but had the problem again of not getting access to this forum, and so consequently also missed your reply as well, June7, ( ........There may be other replies also, but you will not receive any more notifications until you visit the forum again.........)

    I am basically saying the same thing in slightly more detail with the OPs example.
    The TLDR
    _1 As you say, (Paul_Hossler ) on the RHS of the ! you will never need bracketing with ' s, (because it is a macro name that cannot have spaces (As June7 says, they CAN be there) )
    _2 On the LHS of the ! you will need bracketing with ' s in two situations
    _2a) If the workbook name has a space or more in it
    _2b) if you are using a full path, regardless of whether the entire string has any spaces or not. (It seems to be the path that insists on the bracketing with ' s , I have seen that sort of thing in other situations))


    Maybe just for completeness here and posterity I will just drop a link to the reply I had prepared, (as it is a longer reply that tends to increase the likelihood of me getting difficulty in posting due to the current forum software issues here)

    https://www.excelfox.com/forum/showt...ll=1#post25102
    https://www.excelfox.com/forum/showt...age5#post25102

    Alan
    Last edited by DocAElstein; Today at 04:07 AM.

  13. #13
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,891
    Location
    ---------------------------------------------------------------------------------------------------------------------

    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

Posting Permissions

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