【问题标题】:Delete specific number of rows from WORD table从 WORD 表中删除特定数量的行
【发布时间】:2019-03-25 09:55:17
【问题描述】:

我正在创建一个用户表单,供用户在 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),无论行是否为空。

【问题讨论】:

    标签: vba ms-word


    【解决方案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
    

    【讨论】:

    • macropod - 谢谢。代码工作没有错误。我还有一个问题:如何防止用户删除前 6 行;第一行是标题行,接下来的五个我需要一直显示。再次感谢您对此的帮助。
    • 如果您有新问题要问,请提出新问题 - 不要在评论中扩展您想要的内容,@ReyTaino
    • @ReyTaino 您应该在问题中指定所有要求;不要把它们零碎地推出。这是您第二次通过评论更改您的要求。答案已更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多