Consulting

Results 1 to 3 of 3

Thread: Find Replace help

  1. #1
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    215
    Location

    Find Replace help

    I'm trying to get this macro to work. I've wasted so much time. Can someone let me know if it's possible? I'm trying to delete certain words but only from a specified table and column. The selection works but then fails after the first word. Any Ideas?

    Sub DeleteMultipleWords()
    Dim arr() As Variant
    Dim i As Byte
    'Sub PrepareForKeyWordSearch()
    
    j = InputBox("Which Table?")
    l = InputBox("Which Column?")
    selection.Tables(j).Columns(l).Select
    selection.Find.ClearFormatting
    selection.Find.Replacement.ClearFormatting
    arr = Array("for", "and", "not", "nor", "but", "or")
    
      For i = LBound(arr) To UBound(arr)
        With selection.Find
          .Text = arr(i)
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindContinue
        End With
      selection.Find.Execute Replace:=wdReplaceAll
      Next
    End Sub

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,845
    Location
    Try:

    Sub DeleteMultipleWords()
    Dim arrWords()
    Dim lngIndex As Long, lngTable As Long, lngCol As Long, lngRow As Long
    Dim oRngCol As Range, oRng As Range
      lngTable = InputBox("Which Table?")
      lngCol = InputBox("Which Column?")
      arrWords = Array("for", "and", "not", "nor", "but", "or")
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      For lngIndex = LBound(arrWords) To UBound(arrWords)
        With ActiveDocument.Tables(lngTable)
          For lngRow = 1 To .Rows.Count
            Set oRngCol = .Cell(lngRow, lngCol).Range
            Set oRng = .Cell(lngRow, lngCol).Range
            With oRng
              With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = arrWords(lngIndex)
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = True
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                Do While .Execute
                  If oRng.Duplicate.InRange(oRngCol) Then
                    oRng.Text = ""
                    oRng.Collapse wdCollapseEnd
                  Else
                    Exit Do
                  End If
                Loop
              End With
            End With
          Next lngRow
        End With
      Next lngIndex
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    215
    Location
    Perfect, Thanks Greg. Much 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
  •