Consulting

Results 1 to 7 of 7

Thread: Run macro from an already open workbook

  1. #1

    Run macro from an already open workbook

    Hi,

    I need a code to run macro from an already open workbook without giving file name.

    I know there is a code "Application.Run ("'" & workbookname & "'!macroname")" but i donot want to give workbookname as the file name will keep on changing.

    Thanks

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Try:
    [vba]Application.Run "macroname"[/vba]
    Be as you wish to seem

  3. #3
    What i am trying to do is open all the excel files in a folder and run the macro in each file one by one.

    i am stuck here. Need a code to run a macro without mentioning the file name.
    Sub AllFiles()    Dim folderPath As String
        Dim filename As String
        Dim wb As Workbook
      
        folderPath = ThisWorkbook.Sheets("SHeet1").Range("D2").Value
        
        If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
        
        filename = Dir(folderPath & "*.xlsm")
        Do While filename <> ""
          Application.ScreenUpdating = False
            Set wb = Workbooks.Open(folderPath & filename)
    
    
            Application.Run "Committee_Tag"
            filename = Dir
                   
            Loop
         Application.ScreenUpdating = True
        End Sub

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    But you do know the file name so you can simply use it:
    [vba]Application.Run "'" & filename & "'!Committee_Tag"[/vba]
    Be as you wish to seem

  5. #5
    but the file names will keep on changing as i will be opening all the excel files in a folder to run the macro.

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    I know - but your filename variable is the name of the file you opened.
    Be as you wish to seem

  7. #7
    Thank you

Posting Permissions

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