Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 383
  • Last Modified:

Macro to pull multiple files from a directory


Here's the situation.

I have a directory file that has multiple spreadsheets. Each spreadsheet has 3 worksheets within it.
On the second worksheet title "SST" there is data from columns A-I, and a varying amount of rows.

I need a macro that will extract the data from the SST worksheets of all excel files within the directory, and then populate that into a single worksheet.

attached is some code that doesn't work, but is what I'm working with.


Sub Sample()
    Dim wb As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim i As Long, LastRowWs1 As Long, LastRowWs2 As Long
    Dim foundfile As String, pathoffiles As String
    Set ws1 = ActiveWorkbook.Sheets("Sheet1")
    '~~> Change path of the directory here
    pathoffiles = "C:\Temp\"
    foundfile = Dir(pathoffiles & "*.xls") '<~~~  .xlsx of 2007/2010
    Do While Len(foundfile) <> 0
        LastRowWs1 = ws1.Range("A" & Rows.Count).End(xlUp).Row
        Set wb = Workbooks.Open(pathoffiles & foundfile)
        Set ws2 = wb.Sheets("SST")
        LastRowWs2 = ws2.Range("A" & Rows.Count).End(xlUp).Row
        ws2.Range("A1:A" & LastRowWs2).Copy ws1.Range("A" & LastRowWs1)
        wb.Close savechanges:=False
        foundfile = Dir

    Set ws2 = Nothing
    Set wb = Nothing
End Sub

Open in new window

  • 3
  • 2
1 Solution
Dave BrettCommented:
I've modified my Article at http://www.experts-exchange.com/A_2804.html to suit


Public Sub ConsolidateSheets()
    Dim Wb1 As Workbook
    Dim Wb2 As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim ws3 As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim rngArea As Range
    Dim lrowSpace As Long
    Dim lSht As Long
    Dim lngCalc As Long
    Dim lngRow As Long
    Dim lngCol As Long
    Dim X()
    Dim bProcessFolder As Boolean
    Dim bNewSheet As Boolean

    Dim StrPrefix
    Dim strFileName As String
    Dim strFolderName As String

    'variant declaration needed for the Shell object to use a default directory
    Dim strDefaultFolder As Variant

    bProcessFolder = (MsgBox("Process a single folder (Yes)," & vbNewLine & "or single file (No)", vbYesNo, "Application Scope: Folder or single File") = vbYes)
    bNewSheet = (MsgBox("Extract all data to a single sheet (TRUE)," & vbNewLine & "or a target file sheet for each source sheet(FALSE)", vbYesNo, "Output Format: Single sheet or sheet by sheet collection") = vbYes)
    If Not bProcessFolder Then
        If Not bNewSheet Then
            MsgBox "There isn't much point creating a exact replica of your source file :)"
            Exit Sub
        End If
    End If

    'set default directory here if needed
    strDefaultFolder = "C:\temp"

    'If the user is collating all the sheets to a single target sheet then the row spacing
    'to distinguish between different sheets can be set here
    lrowSpace = 1

    If bProcessFolder Then
        strFolderName = BrowseForFolder(strDefaultFolder)
        'Look for xls, xlsx, xlsm files
        strFileName = Dir(strFolderName & "\*.xls*")
        strFileName = Application _
                      .GetOpenFilename("Select file to process (*.xls), *.xls")
    End If

    Set Wb1 = Workbooks.Add(1)
    Set ws1 = Wb1.Sheets(1)
    If Not bNewSheet Then ws1.Range("A1:B1") = Array("workbook name", "worksheet count")

    'Turn off screenupdating, events, alerts and set calculation to manual
    With Application
        .DisplayAlerts = False
        .EnableEvents = False
        .ScreenUpdating = False
        lngCalc = .Calculation
        .Calculation = xlCalculationManual
    End With

    'set path outside the loop
    StrPrefix = strFolderName & IIf(bProcessFolder, "\", vbNullString)

    Do While Len(strFileName) > 0
        'Provide progress status to user
        Application.StatusBar = Left("Processing " & strFolderName & "\" & strFileName, 255)
        'Open each workbook in the folder of interest
        Set Wb2 = Workbooks.Open(StrPrefix & strFileName)
        If Not bNewSheet Then
            'add summary details to first sheet
            ws1.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = Wb2.Name
            ws1.Cells(Rows.Count, "A").End(xlUp).Offset(0, 1) = Wb2.Sheets.Count
        End If
        Set ws2 = Wb2.Sheets("SST")
        If bNewSheet Then
            'All data to a single sheet
            'Skip importing target sheet data if the source sheet is blank
            Set rng2 = ws2.Cells.Find("*", ws2.[a1], xlValues, , xlByRows, xlPrevious)

            If Not rng2 Is Nothing Then
                Set rng1 = ws1.Cells.Find("*", ws1.[a1], xlValues, , xlByRows, xlPrevious)
                'Find the first blank row on the target sheet
                If Not rng1 Is Nothing Then
                    Set rng3 = ws2.Range(ws2.UsedRange.Cells(1), ws2.Cells(rng2.Row, "A"))
                    'Ensure that the row area in the target sheet won't be exceeded
                    If rng3.Rows.Count + rng1.Row < Rows.Count Then
                        'Copy the data from the used range of each source sheet to the first blank row
                        'of the target sheet, using the starting column address from the source sheet being copied
                        ws2.UsedRange.Copy ws1.Cells(rng1.Row + 1 + lrowSpace, ws2.UsedRange.Cells(1).Column)
                        MsgBox "Summary sheet size exceeded. Process stopped on " & vbNewLine & _
                               "sheet: " & ws2.Name & vbNewLine & "of" & vbNewLine & "workbook: " & Wb2.Name
                        Wb2.Close False
                        Exit Do
                    End If
                    'colour the first of any spacer rows
                    If lrowSpace <> 0 Then ws1.Rows(rng1.Row + 1).Interior.Color = vbGreen
                    'target sheet is empty so copy to first row
                    ws2.UsedRange.Copy ws1.Cells(1, ws2.UsedRange.Cells(1).Column)
                End If
            End If
            'new target sheet for each source sheet
            ws2.Copy after:=Wb1.Sheets(Wb1.Sheets.Count)
            'Remove any links in our target sheet
            With Wb1.Sheets(Wb1.Sheets.Count).Cells
                .PasteSpecial xlPasteValues
            End With
            On Error Resume Next
            Wb1.Sheets(Wb1.Sheets.Count).Name = ws2.Name
            'sheet name already exists in target workbook
            If Err.Number <> 0 Then
                'Add a number to the sheet name till a unique name is derived
                    lSht = lSht + 1
                    Set ws3 = Wb1.Sheets(ws2.Name & " " & lSht)
                Loop While Not ws3 Is Nothing
                lSht = 0
            End If
            On Error GoTo 0
        End If

        'Close the opened workbook
        Wb2.Close False
        'Check whether to force a DO loop exit if processing a single file
        If bProcessFolder = False Then Exit Do
        strFileName = Dir

    'Remove any links if the user has used a target sheet
    If bNewSheet Then
        With ws1.UsedRange
            .Cells(1).PasteSpecial xlPasteValues
        End With
        'Format the summary sheet if the user has created separate target sheets
        ws1.Range("A1:B1").Font.Bold = True
    End If

    With Application
        .CutCopyMode = False
        .DisplayAlerts = True
        .EnableEvents = True
        .ScreenUpdating = True
        .Calculation = lngCalc
        .StatusBar = vbNullString
    End With
End Sub

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'From Ken Puls as used in his vbaexpress.com article

    Dim ShellApp As Object
    'Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
                   BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

    'Set the folder to that selected.  (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error GoTo 0

    'Destroy the Shell Application
    Set ShellApp = Nothing

    'Check for invalid or non-entries and send to the Invalid error
    'handler if found
    'Valid selections can begin L: (where L is a letter) or
    '\\ (as in \\servername\sharename.  All others are invalid
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
    Case Else
        GoTo Invalid
    End Select

    Exit Function

    'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False
End Function

Open in new window

Brandon the code that I gave you above was for another question :)

You will have to slightly amend it for this question.

1) Could you confirm the path where the files are kept.
2) Which office version are you using?
3) Is the data in Col A or in other columns as well?

Dave: I see that you have already replied and I am not ignoring your post :)

KnutsonBMAuthor Commented:
awsome. amazing. fabulous.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

KnutsonBM: I will pretend that you missed my post in error :D

Dave BrettCommented:
Thanks KnutsonBM :)

>KnutsonBM: I will pretend that you missed my post in error :D
Sid, but you hadn't actually proferred a solution - you'd asked for more information


>>>Sid, but you hadn't actually proferred a solution - you'd asked for more information

Absolutely! which was missed by the author by mistake ;)


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now