1
votes

I am trying to write a Macro that allows the user to select a range of cells in a Word Table to populate Column 4 with a specific text "SOLD".

Word table

Below is my VBA Macro code so far, but it only types the text in the first Column 4 (rather than the selected range of column 4's)

Dim iSelectionRowEnd As Integer
Dim iSelectionRowStart As Integer
Dim cl1 As Cell
Dim cl2 As Cell
Dim tbl As Table
Dim rng As Range

ActiveDocument.Bookmarks.Add Name:="MacroStartPosition", Range:=Selection.Range

If Selection.Information(wdWithInTable) = False Then
    MsgBox "Selection is not in a table."
Else

    iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
    Selection.Collapse Direction:=wdCollapseStart


    iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)

    Set tbl = Selection.Tables(1)
    Set cl1 = tbl.Cell(iSelectionRowStart, 4)
    Set cl2 = tbl.Cell(iSelectionRowEnd, 4)
    Set rng = cl1.Range.Duplicate
    rng.End = cl2.Range.End
    rng.text = ("SALE")

End If
Selection.Collapse Direction:=wdCollapseStart

If ActiveDocument.Bookmarks.Exists("MacroStartPosition") = True Then
    ActiveDocument.Bookmarks("MacroStartPosition").Select
    ActiveDocument.Bookmarks("MacroStartPosition").Delete
Else
    MsgBox "The original cursor position could not be restored."
End If

Selection.Collapse Direction:=wdCollapseStart

I tried to follow a similar code in Excel and apply it to Word, but I am getting errors. See below Excel.

ActiveCell.FormulaR1C1 = "SALE"
Selection.AutoFill Destination:=Range("D3:D7"), Type:=xlFillDefault
Range("D3:D7").Select
1
What line fails? What is error message?June7
The code works, it just doesn't preform like I hope to. The Intent: Populate ALL column 4 of selected range with "SALE". What is does: Only populates the FIRST column 4 of the selected range.danjedi

1 Answers

1
votes

This works for me. Instead of rng.text = ("SALE") use:

For i = iSelectionRowStart To iSelectionRowEnd
    tbl.Cell(i, 4).Range.Text = "SALE"
Next