2

在我的一个工作表中,我有一个

Private Sub BuggingVba()

应该用一组值替换表中的数据

    Dim MyTable As ListObject, myData() As Variant
    Set MyTable = Me.ListObjects(1)
    myData = collectMyData ' a function defined somewhere else in my workbook

这可能是无关紧要的,但在这样做之前,我调整了列表对象的大小(我逐行扩展,因为如果我一次这样做,我会覆盖我的表格下方的内容而不是移动它。)

    Dim current As Integer, required As Integer, saldo As Integer
    current = MyTable.DataBodyRange.Rows.Count
    required = UBound(sourceData, 1) - LBound(sourceData, 1)
    ' current and required are size of the body, excluding the header

    saldo = required - current

    If required < current Then
        ' reduce size
        Range(DestinBody.Rows(1), DestinBody.Rows(current - required)).Delete xlShiftUp
    Else
        ' expland size
        DestinBody.Rows(1).Copy
        For current = current To required - 1
            DestinBody.Rows(2).Insert xlShiftDown
        Next saldo
    End If

如果要插入任何数据,我会覆盖这些值

    If required Then
        Dim FullTableRange As Range
        Set FullTableRange = MyTable.HeaderRowRange _
            .Resize(1 + required, MyTable.HeaderRowRange.Columns.Count)
        FullTableRange.Value = sourceData
    End If

BAM,我的表/ListObject 不见了! 为什么会发生这种情况,我该如何避免?

End Sub
4

2 回答 2

3
于 2015-09-28T16:41:19.983 回答
-1

旧帖子,但我粘贴在 listobject 表上的方法是删除 databodyrange,将范围设置为数组大小,然后将范围设置为数组。类似于上面提供的解决方案,但不需要调整表格大小。

'Delete the rows in the table
    If lo.ListRows.Count > 0 Then
        lo.DataBodyRange.Delete
    End If

'Assign the range to the array size then assign the array values to the range
    Set rTarget = wsTemplate.Range("A2:K" & UBound(arrTarget) + 1)
    rTarget = arrTarget
于 2018-07-03T20:40:00.797 回答