在空表中插入多行

时间:2019-07-30 00:39:48

标签: excel vba

我正在尝试在表中插入多行。 我一直在尝试使用.DataBodyRange.Rows(0:100).insert,但是问题是.DataBodyRange在表为空(0行)时返回错误。

那么还有其他选择吗?假设我不想在循环中逐行添加。

2 个答案:

答案 0 :(得分:0)

我想我已经解决了我的问题。 添加一行后,databodyrange不再返回错误,并且可以在该行之后插入几行。 尽管必须假设只有1行,但我必须检查rows(1:1)是否返回错误。

                If .ListRows.Count < rowi + 1 Then
                  If .ListRows.Count = 0 Then
                    .ListRows.Add
                  End If
                .DataBodyRange.Rows(.ListRows.Count + 1 & ":" & rowi).Insert Shift:=xlDown

答案 1 :(得分:0)

.DataBodyRangeNothing时,.InsertRowRange是什么东西。

所以您可以做类似的事情

If .DataBodyRange Is Nothing Then
    .InsertRowRange.Resize(100).Insert
Else
    .DataBodyRange.Resize(100).Insert
End If