Solved

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

Posted on 2014-11-20
6
264 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
  • 3
  • 2
6 Comments
 
LVL 46

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 46

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 46

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now