Limiting Find and Replace Format Function in VBA

BrandynBlaze

New Member
Joined
Sep 20, 2012
Messages
29
Hi there!

I'm pretty new to VBA programming, but I built the Find and Replace function below that searches for the value in each row of the first column of sheet 5 and if it finds a match it replaces the format in sheet 5 to the cell in sheet 1.

It works currently, but I'd like to change it in a couple of ways and was hoping someone here would be able to help me.

First, I would like to be able to consider only the first 4 characters of the cell in sheet 1 for a match. This is because it's looking for a customer account number, and the digits after the first 4 are inconsequential but are resulting in false positives.

Secondly/alternatively, I would like to be able to change the font color in sheet one for only the characters that are a match. Currently if there is a match the entire text is changed to that font color, but I'd like to leave the non-matching piece black so we can see what was changed.

Code:
Sub FindAndReplace(Tofind)
      
    Dim cell As Range
    
    
    'With Worksheets("Sheet3")
    Sheet5.Activate
    
        For Each cell In Tofind
            'ToFind is the range of values to replace determined by ReplaceRange()
            
            cell.Select
            'MsgBox ActiveCell.Font.Color
            
            If Not Tofind.Cells.Find(cell.Value, , xlValues, xlPart, , , True) Is Nothing Then 'Double negative, if there is something in ToFind then...
                
 
                With Application.ReplaceFormat.Font
                    .Color = ActiveCell.Font.Color
                End With
                
                With Application.ReplaceFormat.Interior
                    .Color = ActiveCell.Interior.Color
                End With
            
                
                Sheet1.Cells.Replace _
                What:=cell.Value, Replacement:=cell.Value, _
        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=True
       
            End If
        Next cell
    'End With
    Sheet1.Activate
    'Application.ScreenUpdating = False
    
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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
Back
Top