我正在为用户创建一个用户窗体,以便在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)底部开始的行数的预期结果,而不管行是否为空。
发布于 2019-03-23 16:37:37
尝试:
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 Subhttps://stackoverflow.com/questions/55311609
复制相似问题