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".
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