Word VBA Cell Selection Problem

gijimbo

Board Regular
Joined
Feb 3, 2010
Messages
130
I have a word document in which I'm trying to select the first cell of the last row of a table. I keep getting an error (red circle with white 'X') that has no text.
aTcQP.jpg


Here's my code:
Code:
Sub IncrementTable()
ActiveDocument.Unprotect
Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "REV."
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveDown Unit:=wdLine, Count:=1
    Selection.Rows.Delete
    
    ActiveDocument.Tables(4).Rows.Add
    
    MsgBox ("Last row in table 4 = " & ActiveDocument.Tables(4).Rows.Count & vbNewLine & _
            "Last row (alternate) = " & Selection.Information(wdMaximumNumberOfRows))
    
    Dim lastrow As Long
    lastrow = ActiveDocument.Tables(4).Rows.Count
    'Selection.Tables(4).Cell(lastrow, 1).Select
    
    'Selection.MoveDown Unit:=wdLine, Count:=12
    For i = 1 To 7
        Selection.Tables(4).Rows(lastrow).Select
        Selection.Collapse
        Set ffield = Selection.FormFields.Add( _
            Range:=Selection.Range, _
            Type:=wdFieldFormTextInput)
        ffield.Name = ""
        Selection.MoveRight Unit:=wdCell, Count:=1
    Next i
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub

The error is occurring at the "Selection.Tables(4).Rows(lastrow).Select" line but as you can see from the commented areas, I've tried it in other places too with the same results.

Any ideas what I'm doing wrong?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top