在第1行以下添加行,将第一个单元格填充为docvariable,然后递增

时间:2019-04-02 13:56:17

标签: vba ms-word

解决了我自己的问题。我需要首先检查第2列单元是否为空,如果是,则删除该行。这是由于表的最后一行被合并而引起的问题。然后,我需要根据用户选择ArraySize的用户选择,在表的第一行下添加行,以在表中每行维护4列。然后在用户表格中使用docvariable填充每行的第一个单元格,然后在每行中递增数字。然后按降序对表进行排序。这是我的代码,供将来使用。

Private Sub cbArraySize_Click()

If cbArraySize.Value <> 0 Then

DeleteRows
AddRows
AddArrayName
TableSort

End If

End Sub

Sub DeleteRows()

Dim tbl As Word.Table
    Dim nrRows As Long, ColToCheck As Long, i As Long
    Dim cellRange As Word.Range

    Set tbl = ActiveDocument.Tables(2)
    nrRows = tbl.Rows.Count - 1
    ColToCheck = 2

    For i = nrRows To 1 Step -1
        Set cellRange = tbl.Cell(i, ColToCheck).Range
        If Len(cellRange.Text) = 2 Then
            cellRange.Rows(1).Delete
        End If
    Next i

End Sub

Sub AddRows()
With ActiveDocument

.Tables(2).Rows(1).Select
Selection.InsertRowsBelow (cbArraySize.Value)

End With
End Sub

Sub AddArrayName()
With ActiveDocument

Dim tbl As Object
Dim noOfCol As Integer
Dim i As Long
Dim intcount As Integer

Set tbl = .Tables(2)
        With tbl
            noOfCol = tbl.Range.Rows(1).Cells.Count
            For i = .Rows.Count To 1 Step -1
                With .Rows(i)
                    If Len(.Range) = noOfCol * 2 + 2 Then .Cells(1).Range.InsertAfter Text:=tbArrayName.Text + " - " & intcount
                intcount = intcount + 1
                End With
            Next i
End With
End With
End Sub

Sub TableSort()
ActiveDocument.Tables(2).Sort ExcludeHeader:=True
End Sub

1 个答案:

答案 0 :(得分:0)

在原始帖子中发布了我的工作代码。