VBA Search, Replace and Highlight Replaced Cell

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
182
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

Can you possibly help me on this? I'm trying to create a macro wherein in Column A, I need to look for these words Football, Basketball & Tennis and will replace as Sports. In column A as well, I need to look for Math and Science and replace as Subject.

I have below codes for the "Sports" and not sure on how to incorporate it for "Subject"

Sub Test()
'
Dim mapping As Object, itm As Variant


On Error Resume Next


ary = Array("Football", "Baskeball", "Tennis")
For Each wd In ary
Columns("a").REPLACE What:=wd, Replacement:="Sports", SearchOrder:=xlByColumns, MatchCase:=True


Next


Columns("A:A").Select
ActiveCell.FormulaR1C1 = ""
Columns("A:A").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="LGPSCN001", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="STTSTR009", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.End(xlUp).Select
End Sub


This is more on recording so shortcut codes will be much appreciated. :)
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,251
Office Version
  1. 2013
Platform
  1. Windows
Try this:
Code:
Sub Replace_Me()
'Modified 11/14/2018 11:30:42 PM  EST
Application.ScreenUpdating = False
Dim r As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row

    For Each r In Range("A1:A" & Lastrow)
    
        With r.Value
            Select Case r.Value
                Case "Football", "Basketball", "Tennis"
                    r.Value = "Sports"
                Case "Math", "Science"
                    r.Value = "Subject"
            End Select
        End With
    Next
Application.ScreenUpdating = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,162
Messages
5,594,615
Members
413,917
Latest member
devansh02

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top