VBA: Filling Table Cells With Series Number With Mark (.)

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

VBA: Filling Table Cells With Series Number With Mark (.)

Post by Susanto3311 »

hi all.

the code work properly to filling table cells with sequential numbers, like
1
2
3
etc..
i want to modified that code can work addition mark dot (.)
1.
2.
3.

Code: Select all

Sub AddNumbersToTable()
    Dim RowNum As Long
    Dim ColNum As Long
    Dim iStartNum As Integer
    Dim J As Integer

    If Selection.Information(wdWithInTable) Then
        RowNum = Selection.Cells(1).RowIndex
        ColNum = Selection.Cells(1).ColumnIndex

        iStartNum = Val(Selection.Cells(1).Range.Text)
        If iStartNum <> 0 Then
            iStartNum = iStartNum + 1

            For J = RowNum + 1 To ActiveDocument.Tables(1).Rows.Count
                ActiveDocument.Tables(1).Cell(J, ColNum).Range.Text = iStartNum
                iStartNum = iStartNum + 1
            Next
        Else
            MsgBox "Cell doesn't contain a non-zero starting number."
            Exit Sub
        End If
    Else
        MsgBox "Not in table"
    End If
End Sub
how to modified?

susan

User avatar
StuartR
Administrator
Posts: 12604
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: VBA: Filling Table Cells With Series Number With Mark (.)

Post by StuartR »

You could replace the line

Code: Select all

              ActiveDocument.Tables(1).Cell(J, ColNum).Range.Text = iStartNum
with

Code: Select all

              ActiveDocument.Tables(1).Cell(J, ColNum).Range.Text = iStartNum & "."
but it might be easier to simply format the number format for those cells to include the .
StuartR


Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA: Filling Table Cells With Series Number With Mark (.)

Post by Susanto3311 »

hi StuartR, thank you very much! working well