Link to home
Start Free TrialLog in
Avatar of smyers051972
smyers051972Flag for United States of America

asked on

Need to know how to make this script merge multiple excel files BUT insert them by ROW and not by COLUMN

Thanks to Dave for helping me with this code.

I need to know how to make this code merge all excel files by row, currently it merges them by column going right to left, I just need it to merge each file one below the other.

Thanks!

strFolder = "c:\files"
strNewFile = "c:\newfile.xls"
 
    Set objexcel = CreateObject("Excel.Application")
    objexcel.Visible = False
    objexcel.DisplayAlerts = False
 
 
    Set objNewFile = objexcel.Workbooks.Add
    intLastSheet = objNewFile.Worksheets.Count
    If intLastSheet > 1 Then
        For i = 2 To intLastSheet
            objNewFile.Worksheets(2).Delete
        Next
    End If
 
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 
    Set colFileList = objWMIService.ExecQuery _
                      ("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} Where " _
                     & "ResultClass = CIM_DataFile")
 
    For Each objFile In colFileList
        If LCase(objFile.Extension) = "xls" Then
            v = v + 1
 
            Set objWorkbook = objexcel.Workbooks.Open(objFile.Name)
            Set objWorkSheet = objWorkbook.Worksheets(1)
            cCount = objWorkSheet.UsedRange.Columns.Count + objWorkSheet.UsedRange.Cells(1).Column - 1
            For c = cCount To 1 Step -1
                If objexcel.CountA(objWorkSheet.Columns(c)) = 0 Then objWorkSheet.Columns(c).EntireColumn.Delete
            Next
 
            If v = 1 Then
                objWorkSheet.Copy , objNewFile.Worksheets(1)
                objNewFile.Worksheets(1).Delete
            Else
                Set objNewSheet = objNewFile.Worksheets(1)
                lastcol = objNewsheet.UsedRange.Columns.Count + objNewsheet.UsedRange.Cells(1).Column - 1
                objWorkSheet.UsedRange.EntireColumn.Copy objNewSheet.Cells(1, lastcol + 1)
            End If
            objWorkbook.Close
 
        End If
    Next
 
    objNewFile.SaveAs (strNewFile)
    objexcel.Quit

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ExcelGuide
ExcelGuide
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smyers051972

ASKER

Thank you worked perfectly!