我正在创建一个用户表单,供用户在WORD表中插入/粘贴数据。我已经为用户编写了将指定数量的行插入表中的代码,此外,添加时每一行都按顺序编号。我试图反转代码以成功删除指定的行数。
我编写的代码为用户提供了删除指定数量的行的选项的代码无法按我的计划工作。收到消息:参数数量错误或属性分配无效。
Sub DeleteRowsFromTable()
Dim nNumber as Long
Active.document.Tables(2).Select
If Selection.Information(wdWithInTable) = True Then
nNumber = InputBox("Input the number of rows you want to delete:", "Delete rows from the selection")
Selection.Tables(2).Rows.Last.Delete NumRows:=nNumber
end if
end sub
用户希望从表(2)的底部选择行数的预期结果,无论行是否为空。
答案 0 :(得分:1)
尝试:
Sub DeleteRowsFromTable()
Application.ScreenUpdating = False
Dim n As Long, r As Long
With ActiveDocument.Tables(2)
On Error GoTo ErrExit
n = InputBox("Input the number of rows you want to delete:", "Delete rows from the table")
For r = .Rows.Count To 1 Step -1
If n = 0 Then Exit For
If r < 7 Then Exit For
.Rows(r).Delete
n = n - 1
Next
End With
ErrExit:
Application.ScreenUpdating = True
End Sub