将列数据从多张纸复制到同一列中的一张纸,而不会覆盖

时间:2019-03-18 01:00:29

标签: excel vba excel-2010

我有三个数据摘录:

  • 发票
  • 贷方通知单
  • 期刊

我创建了一个Excel工作簿,该工作簿在三个单独的选项卡中具有此数据。每个标签有超过50列。

我要做的是创建一个“主”标签,该标签将这三个标签中的每三个标签中的仅三列数据复制到一个合并的工作表中,即具有以下列:

  • 帐户代码
  • 单位金额
  • 日期

我知道如何使用VBA将整个列从一张纸复制到另一张纸,我只是不知道如何将另一列的数据复制到“主”选项卡上的下一个空白单元格,即不覆盖数据,例如:

我可以将“发票”标签中的帐户代码数据复制到“主”标签中,但是当我从“ CreditNotes”标签中复制帐户代码数据时,它将覆盖我已经从“发票”标签中复制的内容。发票标签,而我希望将其复制到下面。

抱歉,我只是想提供尽可能多的信息!

我的代码是:

Sub Consolidation()

Sheets("Invoices").Columns(11).Copy Destination:=Sheets("master").Columns(1)
Sheets("CreditNotes").Columns(11).Copy Destination:=Sheets("master").Columns(1)
Sheets("Journals").Columns(11).Copy Destination:=Sheets("master").Columns(1)

End Sub

1 个答案:

答案 0 :(得分:0)

合并列

Sub ConsolidateColumn()

    ' Source Worksheet List
    Const cSheets As String = "Invoices,CreditNotes,Journals"
    Const cTarget As String = "Sheet1"  ' Target Worksheet Name
    Const cFur As Long = 2              ' Source First Used Row
    Const cColS As Variant = 11         ' Source Column Letter/Number ("K")
    Const cColT As Variant = 1          ' Target Column Letter/Number ("A")

    Dim rng As Range      ' Source Column Range (in current Source Worksheet)
    Dim vntW As Variant   ' Worksheet Array
    Dim LurS As Long      ' Source Last Used Row Number
    Dim FerT As Long      ' Target First Empty Row Number
    Dim i As Long         ' Worksheet Array Element Counter

    ' Split Source Worksheet List to Worksheet Array.
    vntW = Split(cSheets, ",")

    ' In This Workbook (The Workbook Containing This Code)
    With ThisWorkbook.Worksheets(cTarget)
        ' Loop through elements of Worksheet Array.
        For i = 0 To UBound(vntW)
            ' Calculate Target First Empty Row Number.
            FerT = .Cells(.Rows.Count, cColT).End(xlUp).Row + 1
            ' In Current Source Worksheet
            With ThisWorkbook.Worksheets(Trim(vntW(i)))
                ' Calculate Source Last Used Row Number.
                LurS = .Cells(.Rows.Count, cColS).End(xlUp).Row
                ' Calculate Source Column Range.
                ' Create a reference to Source Column Range.
                Set rng = .Cells(cFur, cColS).Resize(LurS - cFur + 1)
            End With
            ' Calculate Target Column Range.
            ' Copy values from current Source Column Range to Target Column
            ' Range.
            .Cells(FerT, cColT).Resize(rng.Rows.Count) = rng.Value
        Next
    End With
 End Sub