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

Forum statistics

Threads
1,081,468
Messages
5,358,862
Members
400,514
Latest member
JoHio2577

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top