Solved

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

Posted on 2014-11-20
6
285 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 47

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

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 47

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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

739 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