Excel 从其他工作簿向动态表中添加条目

Excel 从其他工作簿向动态表中添加条目,excel,vba,Excel,Vba,大家好,我有一个2020年8月工作簿(first picture.workbook=August 2020,Sheet1)中的条目列表,我想在代表整个发票金额的数据末尾添加到我的动态表中(第二张图片,工作簿=费用,表1。我想设置代码,以便当9月份的数字通过时,我可以将其添加到表的末尾,等等 我的代码如下,(我们现在可以忽略日期列M!) 请注意,我知道我正在将值粘贴到数据的开头(第14行),但是我不确定是否要将其粘贴到表的末尾,并设置代码,以便它能够连续地为新值粘贴。谢谢您这里有一些帮助: Sub

大家好,我有一个2020年8月工作簿(first picture.workbook=August 2020,Sheet1)中的条目列表,我想在代表整个发票金额的数据末尾添加到我的动态表中(第二张图片,工作簿=费用,表1。我想设置代码,以便当9月份的数字通过时,我可以将其添加到表的末尾,等等

我的代码如下,(我们现在可以忽略日期列M!)


请注意,我知道我正在将值粘贴到数据的开头(第14行),但是我不确定是否要将其粘贴到表的末尾,并设置代码,以便它能够连续地为新值粘贴。谢谢您这里有一些帮助:

Sub CopyMonthlyData()
    Dim wb_mth As Workbook, wb_charges As Workbook, mapFromColumn As Variant, mapToColumn As Variant
    Dim lastCell As Integer, i As Integer, nextCell As Integer
    
    Set wb_mth = Workbooks("September 2020")
    Set wb_charges = Workbooks("Charges")
    
    mapFromColumn = Array("A", "P", "Q")
    mapToColumn = Array("A", "J", "K")

        For i = 0 To UBound(mapFromColumn)
            
            With wb_mth.Worksheets(1)
            
                lastCell = .Range(mapFromColumn(i) & .Rows.Count).End(xlUp).Row
                .Range(mapFromColumn(i) & 1 & ":" & mapFromColumn(i) & lastCell).Copy
                
            End With

            With wb_charges.Worksheets(1)
            
                nextCell = .Range(mapToColumn(i) & .Rows.Count).End(xlUp).Row + 1
                .Range(mapToColumn(i) & nextCell).PasteSpecial
            
            End With
            
        Next i
    
End Sub
注释

  • 首先设置两个工作簿的引用。我假设它们都是打开的
  • 创建从每月工作簿到费用工作簿的列映射
  • 在每个映射上循环以复制和粘贴数据

  • 我的朋友,你是如此亲密,你如何编辑代码以从2020年8月的工作表中删除最上面的一行(这样数据头就不会移动)?老实说,如果我能在copy语句中正确地得到这一点,那么用2替换1,使其从第2行开始。这将避免标题。
    Sub CopyMonthlyData()
        Dim wb_mth As Workbook, wb_charges As Workbook, mapFromColumn As Variant, mapToColumn As Variant
        Dim lastCell As Integer, i As Integer, nextCell As Integer
        
        Set wb_mth = Workbooks("September 2020")
        Set wb_charges = Workbooks("Charges")
        
        mapFromColumn = Array("A", "P", "Q")
        mapToColumn = Array("A", "J", "K")
    
            For i = 0 To UBound(mapFromColumn)
                
                With wb_mth.Worksheets(1)
                
                    lastCell = .Range(mapFromColumn(i) & .Rows.Count).End(xlUp).Row
                    .Range(mapFromColumn(i) & 1 & ":" & mapFromColumn(i) & lastCell).Copy
                    
                End With
    
                With wb_charges.Worksheets(1)
                
                    nextCell = .Range(mapToColumn(i) & .Rows.Count).End(xlUp).Row + 1
                    .Range(mapToColumn(i) & nextCell).PasteSpecial
                
                End With
                
            Next i
        
    End Sub