+ Reply to Thread
Results 1 to 2 of 2

Call Macro from Workbook Variable

Hybrid View

  1. #1
    Registered User
    Join Date
    03-06-2007
    Posts
    27

    Call Macro from Workbook Variable

    I have made a loop to open files in a directory but cannot figure out how to run the macro using the workbook variable I have created for the newly opened workbook. In the App.Run line it gives error because it can't find "DataWkbk.xls" but DataWkbk is workbook variable i've defined as the one I just opened not the actual filename. Maybe it should be something like DataWkbk"!Macro". Dunno. Thanks for the help.

    Sub GetDataFromFiles()
    
    Dim i As Integer
    Dim Wkbk As Workbook
    Dim DataWkbk As Workbook
    
    Set Wkbk = ThisWorkbook
    
    With Application.FileSearch
        .NewSearch
        .LookIn = "H:\temp\TEST FOLDER\"
        .FileType = msoFileTypeExcelWorkbooks
        If .Execute > 0 Then
            For i = 1 To .FoundFiles.Count
                Set DataWkbk = Workbooks.Open(.FoundFiles(i))
                Application.Run "DataWkbk!FindToolInfo"
                ActiveWorkbook.Close savechanges:=False
            Next i
        End If
    End With
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Samshut,

    You made a typo. You meant to use the Data workbook's name as a string, not "DataWkBk!".

    Sub GetDataFromFiles()
    
    Dim i As Integer
    Dim Wkbk As Workbook
    Dim DataWkbk As Workbook
    
    Set Wkbk = ThisWorkbook
    
    With Application.FileSearch
        .NewSearch
        .LookIn = "H:\temp\TEST FOLDER\"
        .FileType = msoFileTypeExcelWorkbooks
        If .Execute > 0 Then
            For i = 1 To .FoundFiles.Count
                Set DataWkbk = Workbooks.Open(.FoundFiles(i))
                Application.Run DataWkbk.Name & "!FindToolInfo"
                ActiveWorkbook.Close savechanges:=False
            Next i
        End If
    End With
    End Sub
    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1