Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #18  
Old 03-19-2024, 06:41 AM
gmaxey gmaxey is offline VBA IF Statement Help Windows 10 VBA IF Statement Help Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Shelley,


The double "d" was a typo. Rather than repeating the fourth validator, the fifth validator should have been:
oRng.Characters.Last.Next.Next.Next.Next.Text = " "


Yes, you were very close. So close in fact that I copied your code into the procedure and it worked fine.

Now, when you run the revised code in the attached document you are going to get an error message. I will leave it to you to figure out why and how you want to handle it.

Also, the next suggestion it to break your main procedure up into meaningful chunks. You have already got a process for highlighting your manual CRs. Rather than burying it is a tangle of other code, break it out into a separate procedure. This can make troubleshooting and managing your project easier. E.g.,


Code:
Sub MainProcedure()
  Application.ScreenUpdating = False
  'Do things
  'Do things
  'Call the CompoundCR procedure for the main text then footnotes
  CompoundCRs ActiveDocument.Range
  CompoundCRs ActiveDocument.StoryRanges(wdFootnotesStory)
  'Do things or call other procedures
  Application.ScreenUpdating = True
  Selection.HomeKey Unit:=wdStory
  MsgBox "Complete"
lbl_Exit:
  Exit Sub
End Sub

Sub CompoundCRs(oRngPassed As Range)
Dim strTerms As String
Dim arrTerms() As String
Dim lngIndex As Long, lngOffset As Long
Dim bCompound As Boolean
Dim oRng As Range
  strTerms = "[Aa]rticle,[Aa]ppendix,[Cc]lause,[Pp]aragraph,[Pp]art,[Ss]chedule" 'highlight numbers after these words
  arrTerms = Split(strTerms, ",")
  On Error GoTo Err_Handler
  Set oRng = oRngPassed.Duplicate
  'The reason the one instance with the non-breaking space was missed is because you had a non-breaking and normal space (2 spaces)
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Text = "[ ^s]{2,}"
    .Replacement.Text = " "
    .Execute Replace:=wdReplaceAll
  End With
  For lngIndex = 0 To UBound(arrTerms)
    Set oRng = oRngPassed.Duplicate
    With oRng.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Text = arrTerms(lngIndex) & "[s ^s]@[0-9.]{1,}"
      Do While .Execute
        oRng.Select
        oRng.MoveStart wdWord, 1
        oRng.HighlightColorIndex = wdBrightGreen
        oRng.Collapse wdCollapseEnd
        bCompound = True
        Do While bCompound
          Select Case True
            Case oRng.Characters.Last.Text = "-"
              oRng.Move wdCharacter, 1
              lngOffset = 0
            Case oRng.Characters.Last.Text = "," And oRng.Characters.Last.Next.Text = " "
               oRng.Move wdCharacter, 2
               lngOffset = 0
            Case oRng.Characters.Last.Text = " " And oRng.Characters.Last.Next.Text = "-" And oRng.Characters.Last.Next.Next.Text = " "
              oRng.Move wdCharacter, 3
              lngOffset = 3
            Case oRng.Characters.Last.Text = " " And oRng.Characters.Last.Next.Text = "t" And oRng.Characters.Last.Next.Next.Text = "o" And oRng.Characters.Last.Next.Next.Next.Text = " "
              oRng.Move wdCharacter, 4
              lngOffset = 4
            Case oRng.Characters.Last.Text = " " And oRng.Characters.Last.Next.Text = "o" And oRng.Characters.Last.Next.Next.Text = "r" And oRng.Characters.Last.Next.Next.Next.Text = " "
              oRng.Move wdCharacter, 4
              lngOffset = 4
            Case oRng.Characters.Last.Text = " " And oRng.Characters.Last.Next.Text = "a" And oRng.Characters.Last.Next.Next.Text = "n" And oRng.Characters.Last.Next.Next.Next.Text = "d" And oRng.Characters.Last.Next.Next.Next.Next.Text = " "
              oRng.Move wdCharacter, 5
              lngOffset = 5
            Case oRng.Characters.Last.Text = " " And oRng.Characters.Last.Next.Text = "a" And oRng.Characters.Last.Next.Next.Text = "n" And oRng.Characters.Last.Next.Next.Next.Text = "d" And oRng.Characters.Last.Next.Next.Next.Next.Text = "/" _
                 And oRng.Characters.Last.Next.Next.Next.Next.Next.Text = "o" And oRng.Characters.Last.Next.Next.Next.Next.Next.Next.Text = "r" And oRng.Characters.Last.Next.Next.Next.Next.Next.Next.Next.Text = " "
              oRng.Move wdCharacter, 8
              lngOffset = 8
            Case Else
              bCompound = False
          End Select
          If bCompound Then
            oRng.MoveEnd wdCharacter, 1
            Do While IsNumeric(oRng.Characters.Last.Next) Or (oRng.Characters.Last.Next = "." And IsNumeric(oRng.Characters.Last.Next.Next))
              oRng.MoveEnd wdCharacter, 1
            Loop
            If IsNumeric(oRng.Characters.First.Text) Then oRng.HighlightColorIndex = wdBrightGreen
            oRng.Collapse wdCollapseEnd
            If lngOffset > 0 Then
              oRng.Move wdCharacter, -lngOffset
            End If
          End If
        Loop
      Loop
     End With
Next_For:
  Next
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox Err.Number & " - " & Err.Description
  Resume Next_For
End Sub
Attached Files
File Type: docm Highlight clause references.docm (35.6 KB, 2 views)
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA IF Statement Help IF Statement (again) teza2k06 Excel 8 02-11-2022 08:41 AM
Converting a Select statement in Excel to an update statement shabbaranks Excel Programming 5 10-31-2018 11:47 PM
VBA IF Statement Help Need a little help with an if statement cangelis Excel 2 04-08-2015 05:55 PM
VBA IF Statement Help If statement, may be? Tony Singh Excel 6 03-04-2015 12:52 PM
VBA IF Statement Help Need help with If, Then Statement Please cangelis Excel 4 01-03-2014 09:10 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:29 AM.


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