Consulting

Results 1 to 3 of 3

Thread: Help with merging workbooks

  1. #1

    Help with merging workbooks

    Hi all,

    I have workbooks representing sales by product by month. This means that there is a separate sheet for:

    • sales of soap in January
    • sales of detergent in January
    • sales of soap in February
    • sales of detergent in February
    • ...and so on...


    The column headers in all these files are the same i.e. product name, sales records, date of sale

    I would like to create a new workbook that consolidates all these files in a single sheet. I want the headers to be set at the top once and then all data to be copied as rows below the header. the caveat is that I only want non-blank rows to be copied.

    Can anyone help?

    Thanks a lot in advance
    Michael

  2. #2
    I believe this script by malik641 is very close to what I want to do - the only difference is being that I want all the data as rows in one worksheet whereas this script creates a separate worksheet for each workbook. Can anyone help?

    I can't post links but the kb_id = 829

  3. #3
    I think this code is exactly what I need but I cannot get it to copy only the cells that have values in them (i.e. non-blank cells). Can anyone help?

    Option Explicit
     
    Sub CombineSheetsFromAllFilesInADirectory()
         
        Dim Path            As String 'string variable to hold the path to look through
        Dim FileName        As String 'temporary filename string variable
        Dim tWB             As Workbook 'temporary workbook (each in directory)
        Dim tWS             As Worksheet 'temporary worksheet variable
        Dim mWB             As Workbook 'master workbook
        Dim aWS             As Worksheet 'active sheet in master workbook
        Dim RowCount        As Long 'Rows used on master sheet
        Dim uRange          As Range 'usedrange for each temporary sheet
         
         '***** Set folder to cycle through *****
        Path = ThisWorkbook.Path & "\Templates for captains\" 'Change as needed, ie "C:\"
         
        Application.EnableEvents = False 'turn off events
        Application.ScreenUpdating = False 'turn off screen updating
        Set mWB = Workbooks.Add(1) 'create a new one-worksheet workbook
        Set aWS = mWB.ActiveSheet 'set active sheet variable to only sheet in mWB
        If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
            Path = Path & Application.PathSeparator 'add "\"
        End If
        FileName = Dir(Path & "*.xls", vbNormal) 'set first file's name to filename variable
        Do Until FileName = "" 'loop until all files have been parsed
            If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
                Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
                For Each tWS In tWB.Worksheets 'loop through each sheet
                    Set uRange = tWS.Range("A2", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows _
                    .Count - 1, tWS.UsedRange.Column + tWS.UsedRange.Columns.Count - 1)) 'set used range
                    If RowCount + uRange.Rows.Count > 65536 Then 'if the used range wont fit on the sheet
                        aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
                        Set aWS = mWB.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
                        RowCount = 0 'reset RowCount variable
                    End If
                    If RowCount = 0 Then 'if working with a new sheet
                        aWS.Range("A1", aWS.Cells(1, uRange.Columns.Count)).Value = _
                        tWS.Range("A1", tWS.Cells(1, uRange.Columns.Count)).Value 'copy headers from tWS
                        RowCount = 1 'add one to rowcount
                    End If
                    aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.Count, uRange.Columns.Count).Value _
                    = uRange.Value 'move data from temp sheet to data sheet
                    RowCount = RowCount + uRange.Rows.Count 'increase rowcount accordingly
                Next 'tWS
                tWB.Close False 'close temporary workbook without saving
            End If
            FileName = Dir() 'set next file's name to FileName variable
        Loop
        aWS.Columns.AutoFit 'autofit columns on last data sheet
        mWB.Sheets(1).Select 'select first data sheet on master workbook
        Application.EnableEvents = True 're-enable events
        Application.ScreenUpdating = True 'turn screen updating back on
         
         'Clear memory of the object variables
        Set tWB = Nothing
        Set tWS = Nothing
        Set mWB = Nothing
        Set aWS = Nothing
        Set uRange = Nothing
    End Sub

Posting Permissions

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