Results 1 to 2 of 2

Get Data from Multiple Closed Workbooks

Threaded View

  1. #1
    Forum Contributor
    Join Date
    11-25-2012
    Location
    Edmonton, Alberta
    MS-Off Ver
    2010
    Posts
    146

    Get Data from Multiple Closed Workbooks

    Hi,
    I have a number of directories that I have excel workbooks in. Each workbook is only one sheet, all sheets have a similar name all would be "Don't Unhide Motor Report" followed by numbers from one to 15 and shown on the tab as "(1)", "(2)" etc. to "(15)". See attachment MR CLR650-02 PO#13CJ-122-41683.xlsx

    Now the code below sort of does what I would like it to, I would like it to name all the Workbooks in each directory in column A and then put Cell D6 in Column B. the results of O1 in Column C, the results of O2 in Column D, the results of O3 in Column E.
    Currently it creates a new workbook, then grabs the name of workbook, puts it in Column A then takes D6 from "Sheet1" and puts in Column B, but my sheets are not named sheet1, but when it is it works good.
    Another issue I guess I should mention is my directory would be "L:\Team Folders\Operational\Directional\Clear Operations\Motor Reports\Clear\6.5\CLR650-02". this is the last level but there are also Workbooks in "L:\Team Folders\Operational\Directional\Clear Operations\Motor Reports\" but the "Motor Reports" directory has 11 directories and some of them have multiple directories as well.

    Sub ReadDataFromAllWorkbooksInFolder()
    Dim FolderName As String, wbName As String, r As Long, cValue As Variant
    Dim wbList() As String, wbCount As Integer, i As Integer
    
        FolderName = "C:\users\dave\desktop\New Folder\New Folder"
        ' create list of workbooks in foldername
        wbCount = 0
        wbName = Dir(FolderName & "\" & "*.xls")
        While wbName <> ""
            wbCount = wbCount + 1
            ReDim Preserve wbList(1 To wbCount)
            wbList(wbCount) = wbName 'original line
            wbName = Dir
        Wend
        
            
           If wbCount = 0 Then Exit Sub
        ' get values from each workbook
        r = 0
        Workbooks.Add
        For i = 1 To wbCount
            r = r + 1
            cValue = GetInfoFromClosedFile(FolderName, wbList(i), "Sheet1", "d6") 'original line
            Cells(r, 1).Formula = wbList(i)
            Cells(r, 2).Formula = cValue
        Next i
    End Sub
    
    Private Function GetInfoFromClosedFile(ByVal wbPath As String, _
        wbName As String, wsName As String, cellRef As String) As Variant
    Dim arg As String
        GetInfoFromClosedFile = ""
        If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
        If Dir(wbPath & "\" & wbName) = "" Then Exit Function
        arg = "'" & wbPath & "[" & wbName & "]" & _
            wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
        On Error Resume Next
        GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
    End Function
    Here is workbook that the Macro is in accurals.xlsm
    Last edited by groundin; 04-15-2014 at 02:24 PM. Reason: Add workbook containing Macro

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copying specific data ranges from multiple (closed or in use by others) workbooks
    By TheRetroChief in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-19-2014, 10:39 AM
  2. Vba to extract data from multiple closed workbooks
    By The1theycallp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2013, 07:49 AM
  3. Replies: 0
    Last Post: 07-17-2012, 06:42 PM
  4. Copy Data to One Workbook From Multiple Closed Workbooks
    By Ben4481 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-04-2010, 08:02 AM
  5. [SOLVED] Importing data from multiple closed workbooks
    By Judy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-29-2005, 06:50 PM

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