![]() |
#18
|
||||
|
||||
![]()
Greg, that looks like a better approach. I had a go at minimising the code and avoiding the RegEx which I don't think adds any value to this particular problem.
Code:
Sub HilitePartNumsHyph() Dim rngWord As Range, aRng As Range, sWord As String Dim oRng As Range, oNum As Range, rngFrag As Range, sPref As String Set oRng = ActiveDocument.Range With oRng.Find Do While .Execute(findText:="[0-9]{1,}", MatchWildcards:=True) Set oNum = oRng.Words(1) sWord = UCase(Trim(oNum.Text)) If sWord Like "*[A-Z]*" Then 'If there is a letter included it is a part num sPref = Ucase(oNum.Characters.First.Previous) Do While SwitchHitter(sPref) 'Look back oNum.MoveStart Unit:=wdWord, Count:=-1 Loop If oNum.Text = Trim(oNum.Text) Then 'not ending with a space Do While SwitchHitter(UCase(oNum.Characters.Last.Next)) 'Look forward oNum.MoveEnd Unit:=wdWord, Count:=1 Loop End If oNum.HighlightColorIndex = wdYellow Debug.Print oNum 'This is the output list End If oRng.Start = oNum.End Loop End With End Sub Function SwitchHitter(aChar As String, Optional str As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-") As Boolean 'Returns true if the character appears in provided string SwitchHitter = InStr(str, aChar) > 0 End Function
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
Tags |
numeric, phonetic, word macros |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
rehahm | Word | 4 | 10-07-2015 10:20 AM |
![]() |
shansen | Word | 1 | 03-11-2014 12:43 PM |
Look up cell values with mixed number and letters | Cardinal2 | Excel | 1 | 02-10-2013 01:40 AM |
![]() |
trew | Word | 7 | 11-21-2012 12:54 AM |
![]() |
tanababa | Word | 1 | 04-28-2011 01:35 AM |