Solved

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

Posted on 2014-11-20
6
246 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 45

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

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 45

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel - Scroll Speed 3 24
Auto Operation of MS Excel 2013 2 29
Manipulate Data Quickly 2 13
Automate switch config backup 3 16
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

707 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

16 Experts available now in Live!

Get 1:1 Help Now