Consulting

Results 1 to 2 of 2

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
    444
    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; Yesterday 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.

Posting Permissions

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