Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 10-29-2021, 04:58 PM
davidjm davidjm is offline how do I replace string 123-4 with 123 4 Windows 10 how do I replace string 123-4 with 123 4 Office 2016
Novice
how do I replace string 123-4 with 123 4
 
Join Date: Jun 2018
Posts: 18
davidjm is on a distinguished road
Default

Here's the code:

Code:
Sub perLine()
' Loops thru lines in document passing each line to following Sub ModifyNumbersInText(line)
Static objRegex As Object
Static matches As Object
    If objRegex Is Nothing Then Set objRegex = CreateObject("VBScript.RegExp")
Dim strSample As String

 Dim para As Paragraph
    Dim sentence() As String
    For Each para In ActiveDocument.Paragraphs
          sentence() = Split(para.Range.Text, Chr(11))
            For i = 0 To UBound(sentence)
                  strSample = sentence(i)                  
                      With objRegex
                       .Pattern = "(\-)(\d)"
                       .Global = True
                       .IgnoreCase = True
                       strSample = .Replace(strSample, " X $2")
                      End With
                  ModifyNumbersInText (strSample)
            Next i
    Next
End Sub

Sub ModifyNumbersInText(line)
'Adds 2 to all numerical values in line

Dim txt As String
Dim NumbersFound As String
Dim NumberArray As Variant
txt = line

'Use Regular Expressions to pull out numbers
  Set objRegex = CreateObject("VBScript.RegExp")
  objRegex.IgnoreCase = True
  objRegex.Global = True
  
  objRegex.Pattern = "\d+" 'grab one or more digits
  
  If objRegex.Test(txt) Then
    Set Result = objRegex.Execute(txt)
      If Result.Count > 0 Then
        For Each match In Result
          NumbersFound = NumbersFound & ";" & match.Value
        Next match
    Else
        Exit Sub
      End If
  End If

'Create Array out of Numbers Found
  NumbersFound = Right(NumbersFound, Len(NumbersFound) - 1)
  NumberArray = Split(NumbersFound, ";")
  
'Find/Replace (add 2)
  For x = LBound(NumberArray) To UBound(NumberArray)
    If NumberArray(x) > 30 Then
        txt = Replace(txt, NumberArray(x), NumberArray(x) + 2)
    End If
  Next x

'Output result
    Debug.Print txt
End Sub
This code is mostly tagged together from googling, but it all works apart from numbers pre-pended with "-". When I run
Code:
                  strSample = sentence(i)                  
                      With objRegex
                       .Pattern = "(\-)(\d)"
                       .Global = True
                       .IgnoreCase = True
                       strSample = .Replace(strSample, " X $2")
                      End With
on individual lines it replaces "-" followed by digit with " X " followed by digit.
Running Sub perLine() doesn't do the Regex replace above.

Snippet from Word document that I'm trying to update:

adaptive-expectations hypothesis 261–2, 305
aggregate demand 232–3, 305
aggregate supply 234–7, 305
aggregate supply curve 231
perfectly elastic 235
perfectly inelastic 235–6
aggregates levy 169

Thanks for any help
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I replace string 123-4 with 123 4 Replace characters in a string Marcia Excel Programming 5 05-04-2020 05:15 AM
how do I replace string 123-4 with 123 4 Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
how do I replace string 123-4 with 123 4 Replace multiple strings by one same string at once puff Word 2 02-28-2018 11:04 AM
how do I replace string 123-4 with 123 4 Replace characters in a string Anthon Excel Programming 1 11-03-2016 12:48 AM
How to do multiple find and replace in string. PRA007 Word VBA 2 01-06-2016 09:10 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:40 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft