Excel VBA - 在右侧添加列并填充文本

Excel VBA - add column to right and fill with text

我想编辑我现有的代码(如下),它在 2 个工作簿之间复制和粘贴 tables,以执行以下操作:

  1. 在 "vDB" 的右侧(最后)添加新列,它是 wbTarget
  2. 中的 table 的副本
  3. 用文本填充整个列 "TOTAL"
  4. 然后将其粘贴到另一个名为 "sht1"

    的工作簿
    Dim vDB As Variant
    Dim sht1 As Worksheet
    Dim wbTarget As Workbook 
    
    'if sheet name is "TOTAL" then...
    If "TOTAL" = Sheet.Name Then
    
        'copy a given range from the target workbook
        vDB = wbTarget.Sheets(Sheet.Name).Range("b3:i102")
    
        'paste that range under existing data in the master workbook
        sht1.Range("a" & Rows.Count).End(xlUp)(2).Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
    
    End If
    

我失败的尝试:

    Dim vDB As Variant
    Dim sht1 As Worksheet
    Dim wbTarget As Workbook 

    If "TOTAL" = Sheet.Name Then

        vDB = wbTarget.Sheets(Sheet.Name).Range("b3:i102")

        With vDB
        .ListColumns.Add
        x = .ListColumns.Count
        .Range(2, x) = "Total"
        End With

        sht1.Range("a" & Rows.Count).End(xlUp)(2).Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB

    End If

我收到 "Object required" 错误。有什么建议么?我是 vba...

的新手

在当前代码末尾添加以下代码行:

' adding "TOTAL" to last column on the right
sht1.Range("A1").Offset(, UBound(vDB, 2)).Resize(UBound(vDB, 1) + 1, 1).Value2 = "TOTAL"