Excel:编写整个ListObject列

时间:2019-02-22 14:00:05

标签: excel vba listobject

如何简单地将数组中的值一次写入整个ListObject列(仅数据行,不包括标题)?我找到了解决方案,但我认为这不是很漂亮:

' needs: ActiveSheet with ListObject "Tabelle1",
' with second column heading "Spalte2" and 4 data rows
Sub WriteLoColumnTest()
    Dim lo As ListObject
    Set lo = ActiveSheet.ListObjects("Tabelle1")

    Dim va(1 To 4, 1 To 1) As Variant
    va(1, 1) = "a"
    va(2, 1) = "b"
    va(3, 1) = "c"
    va(4, 1) = "d"

    ' here it comes:
    lo.ListColumns("Spalte2") _
        .Range _
        .Resize(lo.ListRows.Count) _
        .Offset(1) _
        .Value2 = va
End Sub

我需要.ListColumns.Range来获取我的范围,并且需要.Resize.Offset来跳过标题行。有没有更简单的方法?

1 个答案:

答案 0 :(得分:2)

如果您正在寻找一种更简单的方法,则可以使用以下方法实现相同的目的

Sub WriteLoColumnTest()
    Dim lo As ListObject: Set lo = ActiveSheet.ListObjects("Tabelle1")
    Dim va As Variant

    va = Array("a", "b", "c", "d")
    lo.ListColumns("Spalte2").DataBodyRange.Value2 = Application.Transpose(va)
End Sub

为进一步阅读,您可能需要熟悉listobject的不同部分

https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables