Macro to search for a number and change it "red"

MikeyZ

Well-known Member
Joined
Mar 14, 2002
Messages
553
I have a string of numbers in a cell and I need a macro to search for the number 530BA and turn it red.

numbers look like the following: 422EDA 651GCB, 525TAA, 530ZBA, 411ZXP, 662MGA, 422EDA, 530ZBA, 411ZXF, 420LDA, 525KEA.

There could be 20+ number in a cell.

Is there simple macro I can run to do the trick?
 

davewatson86

New Member
Joined
Jul 8, 2019
Messages
30
Try this

Code:
Option Explicit
Dim FindString As String
Dim Rng As Range
Sub Find_First()
    
    
    
    'use FindString = InputBox("Enter a Search value") if the value you want to find changes
    'or FindString = range("A1").value for when you want to find the value entered in cell A1
    
    FindString = "530BA"
    
    If Trim(FindString) <> "" Then
        With Sheets("Sheet1").Range("AD1", Range("ad" & Cells.Rows.Count).End(xlUp))
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Rng.Interior.Color = RGB(255, 0, 0)
                Rng.Select
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If
End Sub
 

JugglerJAF

Active Member
Joined
Feb 17, 2002
Messages
264
I've just tested this and it seems to do what you need.

As always, please test on a copy of your data file before using on live data just in case of unexpected results!

Code:
Sub set_to_red()
'reset font colour of selection to automatic
    Selection.Font.ColorIndex = xlAutomatic
'specify string to search for
    search_string = "530BA"
'loop through each cell in range and set search string to red if found
    Application.ScreenUpdating = False
    For Each c In Selection
        search_string_start = InStr(1, UCase(c), search_string)
        search_string_length = Len(search_string)
        If search_string_start > 0 Then
            c.Characters(Start:=search_string_start, Length:=search_string_length).Font.ColorIndex = 3
        End If
    Next c
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,280
Office Version
365
Platform
Windows
If you can have the string more than once in a cell, try
Code:
Sub MikeyZ()
   Dim Cl As Range
   Dim Strt As Long
   Dim Srch As String
   
   Srch = "530ZBA"
   For Each Cl In Range("AD2", Range("AD" & Rows.Count).End(xlUp))
      Strt = InStr(1, Cl, "530ZBA", vbTextCompare)
      Do Until Strt = 0
         Cl.Characters(Strt, Len(Srch)).Font.Color = vbRed
         Strt = InStr(Strt + Len(Srch), Cl, Srch, vbTextCompare)
      Loop
   Next Cl
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,280
Office Version
365
Platform
Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,077,919
Messages
5,337,183
Members
399,131
Latest member
Vinnyjuice

Some videos you may like

This Week's Hot Topics

Top