Thread: [Solved] Find Challenge
View Single Post
 
Old 01-06-2025, 03:21 PM
batman1 batman1 is offline Windows 11 Office 2013
Advanced Beginner
 
Join Date: Jan 2025
Posts: 57
batman1 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
This post is a continuation of a discuss that started here:
https://www.msofficeforums.com/word-...x-pattern.html

Code:
Sub ScratchMacroC()
Dim oRng As Range
Set oRng = ActiveDocument.Range
Dim lngIndex As Long
  With oRng.Find
    .Text = "[0-9]{1,}"
    .MatchWildcards = True
    While .Execute
      lngIndex = 1
      If InStr(Left(oRng.Text, 4), "2") > 0 Then
        Select Case Len(oRng.Text)
          Case Is > 4
            Debug.Print Left(oRng.Text, 5)
            lngIndex = 5
          Case Is = 4
            Debug.Print oRng.Text
            lngIndex = 4
        End Select
      End If
      oRng.End = oRng.Start + lngIndex
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
I tested it with some data and the code works fine.

In my post #26 I quoted vivek (he later deleted it): "Sometimes regex is more functional than Word standard instruments". And in my opinion there is a code with Regex simpler than Sub ScratchMacroC
Reply With Quote