VBA code that formats a word (that word only) if it is present in each cell of a specified range

Posted on 2011-03-03
Last Modified: 2012-05-11
How do I write a code that says in effect: Look for the string "27D" inside each cell in a specidied range, and make that string Red Bold? This sample code is just to give you a better idea of what I'm looking for


Sub FormatText()
Dim str As String
'str = find "27D" within the cell and make it Red Bold
For Each cell In Range("B2:B200")
str.Font.ColorIndex = 3 'I know this is all wrong
str.Bold = True 'I know this is all wrong

End Sub

Open in new window

Question by:gabrielPennyback
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
  • 2
LVL 30

Accepted Solution

SiddharthRout earned 500 total points
ID: 35029686
Is this what you want? Please replace Sheet1 with the relevant sheet name.

Sub ColorCells()
    Dim SearchString As String
    Dim aCell As Range, bCell As Range
    Dim ExitLoop As Boolean
    SearchString = "27D"
    Set aCell = Sheets("Sheet1").Cells.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    If Not aCell Is Nothing Then
        Set bCell = aCell
        aCell.Font.ColorIndex = 3
        aCell.Font.Bold = True
        Do While ExitLoop = False
            Set aCell = Sheets("Sheet1").Cells.FindNext(After:=aCell)

            If Not aCell Is Nothing Then
                If aCell.Address = bCell.Address Then Exit Do
                aCell.Font.ColorIndex = 3
                aCell.Font.Bold = True
                ExitLoop = True
            End If
        MsgBox SearchString & " not Found"
    End If
End Sub

Open in new window


Author Comment

ID: 35031275
Hi Sid, thanks. This works great for coloring the entire cell, but I need something that only highlights "27D". I was going to post a comment but I couldn't get myself to stop trying to figure it out, and I managed to come with something surprisingly compact that works great.

I never would have gotten this except for your post, so thanks.

 - John
Sub FormatText()
Dim cel As Range, str As String
str = [G1].Value
For Each cel In Range("D8:D194")
If InStr(cel.Value, str) <> 0 Then
cel.Characters(Start:=InStr(cel.Value, str), Length:=3).Font.Bold = True
cel.Characters(Start:=InStr(cel.Value, str), Length:=3).Font.ColorIndex = [G1].Font.ColorIndex
cel.Offset(0, -3).Interior.ColorIndex = 45
End If
End Sub

Open in new window


Author Closing Comment

ID: 35032656
I tried closing this 2 hours ago but I couldn't. Thanks again!

- John

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

688 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