Solved

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
3
209 Views
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

Thanks,
John

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

Next
End Sub

Open in new window

0
Comment
Question by:gabrielPennyback
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
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
            Else
                ExitLoop = True
            End If
        Loop
    Else
        MsgBox SearchString & " not Found"
    End If
End Sub

Open in new window


Sid
0
 
LVL 1

Author Comment

by:gabrielPennyback
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
Next
End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

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

- John
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
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 will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

744 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