简体   繁体   中英

Combining data from multiple workbooks into a single worksheet

My code is designed to allow the user to open multiple workbooks and take the data from each workbook copy it in to a new workbook and save that workbook in a designated location with a dynamic name.

My code is failing when the data that has been copied from the opened workbooks in to the new workbook.

Option Explicit
Option Base 1

Sub ConslidateWorkbooks()

Dim Filename As Variant, nw As Integer
Dim i As Integer, A() As Variant
Dim tWB As Workbook, aWB As Workbook, nWB As Workbook
Dim Sheet As Worksheet
Dim strFullname As String

Set tWB = ThisWorkbook
strFullname = "G:\CMG\DCM\Operations\Monthly Cycle\Monthly Transaction Upload\" & Range("PB") & "\" & Format(Range("CurrentDate"), "yyyy") & "\Raw Files\" & "Raw File - " & Range("PB") & Format(Range("CurrentDate"), "mmddyy") & ".csv"


Filename = Application.GetOpenFilename(FileFilter:="Excel Filter(*.csv), *.csv", Title:="Open File(s)", MultiSelect:=True)

'Application.ScreenUpdating = False

nw = UBound(Filename)
ReDim A(nw)
    For i = 1 To nw
        Workbooks.Open Filename(i)
        Set aWB = ActiveWorkbook
        A(i) = aWB.Sheets(1).Range("A6:L" & Cells(Rows.Count, 2).End(xlUp).Row)
        aWB.Close SaveChanges:=False

    Next i

Set nWB = Workbooks.Add
nWB.Activate
nWB.Sheets(1).Range("A1:L" & Cells(Rows.Count, 2).End(xlUp).Row) = WorksheetFunction.Transpose(A)
nWB.SaveAs Filename:=strFullname, FileFormat:=xlCSV, CreateBackup:=True
nWB.Close

'Application.ScreenUpdating = True

End Sub

I am expecting the data from each workbook (my test case is 4 separate workbooks, each with 1 sheet, all with a different number of rows but the exact number of columns (AL)) to be copied in to a single sheet of a newly created workbook (consecutively copied). I am receiving a

Run Time Error 13 Type Mismatch

on the following line of code:

nWB.Sheets(1).Range("A1:L" & Cells(Rows.Count, 2).End(xlUp).Row) = WorksheetFunction.Transpose(A)

Something more like this:

Sub ConslidateWorkbooks()

    Dim Filename As Variant, nw As Long
    Dim i As Long, A() As Variant
    Dim tWB As Workbook, aWB As Workbook, nWB As Workbook, wb As Workbook
    Dim Sheet As Worksheet, arr
    Dim strFullname As String

    Set tWB = ThisWorkbook

    'all the ranges here should have workbook/worksheet qualifiers...
    strFullname = "G:\CMG\DCM\Operations\Monthly Cycle\Monthly Transaction Upload\" & _
               Range("PB") & "\" & Format(Range("CurrentDate"), "yyyy") & "\Raw Files\" & _
               "Raw File - " & Range("PB") & Format(Range("CurrentDate"), "mmddyy") & ".csv"


    Filename = Application.GetOpenFilename(FileFilter:="Excel Filter(*.csv), *.csv", _
                                           Title:="Open File(s)", MultiSelect:=True)

    nw = UBound(Filename)
    ReDim A(1 To nw) 'specify lower bound

    For i = 1 To nw
        Set aWB = Workbooks.Open(Filename(i))
        With aWB.Sheets(1)
            A(i) = .Range("A6:L" & .Cells(.Rows.Count, 2).End(xlUp).Row)
            .Parent.Close SaveChanges:=False
        End With
    Next i

    Set nWB = Workbooks.Add()

    With nWB.Sheets(1)
        'loop over the A array, and add each contained array to the sheet
        For i = 1 To nw
            arr = A(i)
            .Cells(.Rows.Count, 2).End(xlUp).Offset(1, 0).Resize( _
                          UBound(arr, 1), UBound(arr, 2)).Value = arr
        Next i
        .Rows(1).Delete 'remove empty first row
    End With

    nWB.SaveAs Filename:=strFullname, FileFormat:=xlCSV, CreateBackup:=True
    nWB.Close False

End Sub

replace this

nWB.Sheets(1).Range("A1:L" & Cells(Rows.Count, 2).End(xlUp).Row) = WorksheetFunction.Transpose(A)

with this

Dim OutputRow as long
  for i = 1 to nw
    with nwb
      OutputRow = .Cells(.Rows.Count, 1).Resize(1, 2).End(xlUp).Row + 1
      .cells(OutputRow, 1).resize(ubound(a(i), 1), 2).value = a(i)
    end with

  next i

You need to iterate through the array of arrays you collected. I removed the transpose function because based on your description, you just want to collect all of the data from the other worksheet's tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM