vbams-wordrowwildcardword-table

Word VBA Find Using Wildcards


I have a word table and it is having 2000 rows. Each row contains some EXTENT i.e. area of land in square yards(Sq. Yds) from 10 Sq. yds to 70000 sq. yds. I have to filter it, need rows having Extent more than 500 Sq. Yds. Among 2000 rows I want to filter these rows using Wildcards in VBA Word Macro in such a way that I shall get Extent of 500 and more than it leaving rows below 500 Sq. yds. The text to be found is combination of characters and number. I want to filter finding "EXTENT: ([5-9][0-9][0-9])". "EXTENT: XXXX"(number digits).

Sub FilterExtentUsingWildcards()
  Application.ScreenUpdating = False
  Dim TblRng As Range, TmpRng As Range
  With ActiveDocument.Tables(1)
    Set TblRng = .Range: Set TmpRng = .Range
    With .Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<EXTENT:><space>([3-9][0-9][0-9])" 'FindText which is combination of
        'characters, space and Number
        .MatchWildcards = True                'i.e. "EXTENT: XXXX(number digits)
        .Replacement.Text = ""
        .Forward = True
        .Format = False
        .Wrap = wdFindStop
        .MatchCase = True
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        If .InRange(TblRng) Then
          TmpRng.Collapse wdCollapseEnd
          TmpRng.FormattedText = .Rows(1).Range.FormattedText
          .Rows(1).Delete
        End If
        .Find.Execute
      Loop
    End With
    If .Rows.Count > TblRng.Rows.Count Then
      .Split .Rows(TblRng.Rows.Count + 1)
    End If
  End With
  Application.ScreenUpdating = True
End Sub

Solution

  • The comment in your code says that you are looking for "EXTENT: 300" and above, but Find.Text doesn't include the space. You then turn MatchWildcards on and 7 lines later turn it off.

    I have edited your code as below:

    Sub FilterExtentUsingWildcards()
      Application.ScreenUpdating = False
      Dim TblRng As Range, TmpRng As Range
      With ActiveDocument.Tables(1)
        Set TblRng = .Range: Set TmpRng = .Range
        With .Range
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .text = "EXTENT: ([5-9][0-9][0-9])" 'FindText which is combination of characters, space and Number
            .MatchWildcards = True 'i.e. "EXTENT: XXXX(number digits)
            .Replacement.text = ""
            .Forward = True
            .Format = False
            .Wrap = wdFindStop
            .MatchCase = True
            .MatchWholeWord = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
          End With
          Do While .Find.Found
            If .InRange(TblRng) Then
              TmpRng.Collapse wdCollapseEnd
              TmpRng.FormattedText = .Rows(1).Range.FormattedText
              .Rows(1).Delete
            End If
            .Find.Execute
          Loop
        End With
        If .Rows.Count > TblRng.Rows.Count Then
          .Split .Rows(TblRng.Rows.Count + 1)
        End If
        'uncomment the next line if you want to delete the original table
        '.Delete
      End With
      Application.ScreenUpdating = True
    End Sub
    

    Before: enter image description here

    After: enter image description here