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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,225,551
Messages
6,185,605
Members
453,307
Latest member
addydata

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