Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-11-20
6
Medium Priority
?
313 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 49

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 93

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
Industry Leaders: 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 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

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

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!

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

722 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