Consulting

Results 1 to 3 of 3

Thread: VB code to search and modify text in the first column of multiple tables

  1. #1
    VBAX Regular
    Joined
    Sep 2016
    Posts
    7
    Location

    VB code to search and modify text in the first column of multiple tables

    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?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Sep 2016
    Posts
    7
    Location
    Quote Originally Posted by gmaxey View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •