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
265
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
32,151
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
32,151
Office Version
365
Platform
Windows
Glad we could help & thanks for the feedback
 

Forum statistics

Threads
1,081,855
Messages
5,361,714
Members
400,648
Latest member
dalviaks

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top