Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2020, 10:56 PM
Guessed's Avatar
Guessed Guessed is offline Help creating a macro to identify values that have letters and numbers. Windows 10 Help creating a macro to identify values that have letters and numbers. Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply

Tags
numeric, phonetic, word macros

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help creating a macro to identify values that have letters and numbers. Creating your own Section Headings using Numbers and letters and linking to the TOC rehahm Word 4 10-07-2015 10:20 AM
Help creating a macro to identify values that have letters and numbers. Creating a TOC with sequential letters instead of page numbers at the right margin 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
Help creating a macro to identify values that have letters and numbers. Captions mixing letters and numbers trew Word 7 11-21-2012 12:54 AM
Help creating a macro to identify values that have letters and numbers. Creating TOC with letters and numbers tanababa Word 1 04-28-2011 01:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:42 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