Thread: [Solved] VBA IF Statement Help
View Single Post
 
Old 03-20-2024, 01:19 PM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Expert
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA IF Statement Help

Hi Greg, yes that has definitely worked, thank you so much. Thank you for suggesting I split the previous code into a few sections so I can identify any errors easily. I've run each section individually to see how they perform before updating the MainProcedure code with the various Calls and for some reason, the CompoundCRs code is changing spaces after periods at the end of sentences from two spaces to one space (our housestyle is two spaces). I can't see why this is happening though.

I've been trying to update the CompoundCRs code to include if there is a space or non-breaking space present but nothing has worked yet so I remembered what you said in an earlier post that sometimes its best to remove something and put it back in at the end, so I've added a find and replace in the CompoundCRs Code to remove any non-breaking spaces associated with the cross references and created a new Call to reinstate the non-breaking spaces at the end of the process.

I can't thank you enough for your help on this, it will really help me a lot when housestyling documents going forward.

Code:
Sub DPU_ReinstateNonBreakingSpaces_CRs()
Dim oRng As Range, fld As Field, sFind1 As String, arr() As String, i As Long
sFind1 = "[Aa]rticle [Aa]rticles [Aa]ppendix [Aa]ppendices [Cc]lause [Cc]lauses [Pp]aragraph [Pp]aragraphs [Pp]art [Pp]arts [Ss]chedule [Ss]chedules [Ss]ection [Ss]ections Act [Rr]egulation [Rr]egulations [Oo]rder [Rr]ule [Rr]rules"
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
    .Replacement.ClearFormatting
    .Format = False
    .Forward = True
    .Wrap = wdFindContinue
    .MatchWildcards = True
arr = Split(sFind1, " ")
    For i = 0 To UBound(arr)
   .text = "(" & arr(i) & ") ([0-9.]{1,})"     'NBS for clause etc. references in the array
   .Replacement.text = "\1^s\2"
   .Execute Replace:=wdReplaceAll
    Next
    For Each fld In oRng.Fields                     'Spaces before auto cross-refs are NBS
            If fld.Type = wdFieldRef Then
                If Not fld.Result.Previous Is Nothing Then
                    Set oRng = fld.Result.Previous.Characters(1)
                    If oRng.text = Chr(32) Then oRng.text = Chr(160)
                End If
            End If
  Next
  End With
End Sub
Reply With Quote