Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-13-2024, 02:06 PM
Shelley Lou Shelley Lou is offline VBA IF Statement Help Windows 10 VBA IF Statement Help Office 2016
Competent Performer
VBA IF Statement Help
 
Join Date: Dec 2020
Posts: 171
Shelley Lou is on a distinguished road
Default VBA IF Statement Help

I have created a macro to help me when formatting documents. The code finds instances of rogue punctuation, mostly ones that are bold and highlights them bright green so I can easily identify what I need to reformat in line with housestyle. I've got the code to also not highlight bold punctuation if contained within a bold heading or part of a bold definition through the IF Statement.



I'm struggling of what to add to the IF Statement to capture other instances e.g. more than one bold punctuation together (see 1.8 of the attached document where I've listed a few examples). Would appreciate any advice.

Bold Punc but not within Bold Text.docx

Code:
Sub HighlightBoldPuncDemo1()
Application.ScreenUpdating = False
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Highlight = True 'Activate replacement highlighting
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    Options.DefaultHighlightColorIndex = wdBrightGreen
    .text = Chr(34)  'Highlight non-bold straight quotes
    .Font.Bold = False
    .Execute Replace:=wdReplaceAll
    .text = "[[\]^0145^0146^0147^0148]{1,}" 'Highlight non bold curly quotes/apostrophes/square brackets
    .Execute Replace:=wdReplaceAll
    .text = "[" & Chr(44) & Chr(145) & Chr(146) & Chr(147) & Chr(148) & "\(\)\[\]\:\;\.\'\,]" 'Highlight bold curly quotes/apostrophes, brackets/square brackets, comma etc.
    .Font.Bold = True
    .Execute Replace:=wdReplaceAll
End With
With oRng.Find
    .text = "[" & Chr(34) & "\(\)\[\]\:\;\.\'\,]"  'highlight bold straight quotes if word is plain text but don't highlight if all bold
    .Font.Bold = True
    While .Execute
      oRng.Select
      On Error Resume Next
      If oRng.Characters.Last.Next.Font.Bold = False Then oRng.HighlightColorIndex = wdBrightGreen 'highlight if bold but not within a heading or definition
      If oRng.Characters.Last.Previous.Font.Bold = True Then oRng.HighlightColorIndex = wdNoHighlight 'don't highlight if contained within a bold heading
    Wend
  End With
End Sub
Reply With Quote
 



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 03:13 AM.


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