PDA

View Full Version : VB code to search and modify text in the first column of multiple tables



hubs
09-03-2016, 08:42 AM
Hi,

I have got a Word document with several tables, and in between each table is text.

I need to be able to search the first column of each table, and replace any blank entries in the first column of each table with "Person A", and to replace text in just the first column which has "Me" to "Person B".

Is this possible, any help would be very much appreciated? :bug:

gmaxey
09-03-2016, 09:15 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Word.Table
Dim lngRow As Long
For Each oTbl In ActiveDocument.Tables
For lngRow = 1 To oTbl.Rows.Count
If Len(oTbl.Cell(lngRow, 1).Range.Text) = 2 Then
oTbl.Cell(lngRow, 1).Range.Text = "Person A"
End If
If InStr(UCase(oTbl.Cell(lngRow, 1).Range.Text), "ME") > 0 Then
oTbl.Cell(lngRow, 1).Range.Text = "Person B"
End If
Next
Next
lbl_Exit:
Exit Sub

End Sub

hubs
09-03-2016, 10:23 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Word.Table
Dim lngRow As Long
For Each oTbl In ActiveDocument.Tables
For lngRow = 1 To oTbl.Rows.Count
If Len(oTbl.Cell(lngRow, 1).Range.Text) = 2 Then
oTbl.Cell(lngRow, 1).Range.Text = "Person A"
End If
If InStr(UCase(oTbl.Cell(lngRow, 1).Range.Text), "ME") > 0 Then
oTbl.Cell(lngRow, 1).Range.Text = "Person B"
End If
Next
Next
lbl_Exit:
Exit Sub

End Sub



Greg, many thanks, that worked perfectly, that was hugely appreciated :)