Solved

How to create a VBA code to automatically highlight certain text in a cell?

Posted on 2014-11-20
6
298 Views
Last Modified: 2014-11-21
I need a code to run on sheet1 that will automatically highlight the following text within a very long string in cell A1.  The code can’t search for case sensitive.  I’m running Excel 2010

WIP
SUSPENDED
PC
Called
0
Comment
Question by:kbay808
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40456533
You can modify this to meet your needs, changing the range of course and Underline to bold.
Sub HighLightCell()
Dim intStart As Integer
Dim intEnd As Integer
intStart = 1
Do
    intStart = InStr(intStart, Range("M15").Value, "Ticket-")
    If intStart > 0 Then
        intEnd = InStr(intStart, Range("M15").Value, ") - ")
        Range("M15").Characters(intStart, intEnd - intStart + 3).Font.Underline = True
        intStart = intStart + 1
    End If
Loop Until intStart = 0
End Sub

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 40456685
No points, please :)

It's not clear if you want a case sensitive match or not.  You should specify in the InStr() calls whether to use a binary compare (case sensitive) or a text compare.  If you don't, then you get whatever the module-level default is (unless otherwise specified, case sensitive).
0
 

Author Comment

by:kbay808
ID: 40456743
I don’t know how to modify the above code to highlight instead of underling the text.  To better explain what I’m looking for, I attached an example file.  The example shown on the right shows exactly what I’m trying to accomplish.
Example.xlsm
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 48

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40457588
Sub HighLightCell()
Dim intStart As Integer
Dim intEnd As Integer
intStart = 1
Do
    intStart = InStr(intStart, LCase(Range("A3").Value), "wip")
    If intStart > 0 Then
        Range("A3").Characters(intStart, 3).Font.Color = vbRed
        intStart = intStart + 1
    End If
Loop Until intStart = 0

intStart = 1
Do
    intStart = InStr(intStart, LCase(Range("A3").Value), "suspended")
    If intStart > 0 Then
        Range("A3").Characters(intStart, 9).Font.Color = vbRed
        intStart = intStart + 1
    End If
Loop Until intStart = 0

intStart = 1
Do
    intStart = InStr(intStart, Range("A3").Value, "PC")
    If intStart > 0 Then
        Range("A3").Characters(intStart, 2).Font.Color = vbRed
        intStart = intStart + 1
    End If
Loop Until intStart = 0

End Sub

Open in new window

I did this for A3 for my testing. Do you want the stars around the second "suspended"? Also if you want "pc" to be made red like "PC", add the LCase as in the other two.
0
 

Author Closing Comment

by:kbay808
ID: 40457714
That worked perfectly!!!  Thank you very much.
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 40457719
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question