replace text only if the text is complete string in cell

kwagner1

Active Member
Joined
Jun 10, 2003
Messages
445
Is there anyway to do a find/replace that will replace the text if, and only if, the text being replaced is the complete string in the cell (i.e. exact match).
Example:
ColA in my worksheet may contain data as follows:
AI1234
AI123456

I want my VB routine to always replace AI1234 with "ID25" and I always want to replace AI123456 with "ID33" (simple enough). What i don't want is the "AI123456" to become "ID2556" when the command to replace AI1234 executes.

I'd like to do this using VB code.

Thanks!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi
1) Replace AI123456 with ID33
2) Replace AI1234 with ID25

so that ID2556 will never happen.
 
Upvote 0
You could check the "find entirecells only" box in the Replace from the Edit Menu. If you want VB, this shows the syntax you want, specificaly the LookAt argument.

Code:
Selection.Replace What:="aa", Replacement:="x", LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=True
 
Upvote 0
thanks guys. Mike - i think i'll take your approach.. the LookAt:=xlWhole appears to be what I need...

thanks guys!
 
Upvote 0
Hi kwagner1:

How about using ...

Code:
Range("A:A").Replace What:="AI1234", Replacement:="ID25", LookAt:=xlWhole
Range("A:A").Replace What:="AI123456", Replacement:="ID33", LookAt:=xlWhole
 
Upvote 0
kwagner1,

Try this:

Code:
Option Explicit
Sub Test()
    Dim i, j As Long
    Application.ScreenUpdating = False
    i = Range("A" & Rows.Count).End(xlUp).Row
    'I want my VB routine to always replace AI1234 with "ID25" and I always want to replace AI123456 with "ID33"
    For j = 1 To i
        With Cells(j, 1)
            .Value = Replace(Cells(j, 1), "AI123456", "ID33")
        End With
    Next j
    For j = 1 To i
        With Cells(j, 1)
            .Value = Replace(Cells(j, 1), "AI1234", "ID25")
        End With
    Next j
    Application.ScreenUpdating = True
End Sub

Have a greaqt day,
Stan
 
Upvote 0
Stan,
2nd loop is not needed
Code:
Option Explicit
Sub Test()
    Dim i, j As Long
    Application.ScreenUpdating = False
    i = Range("A" & Rows.Count).End(xlUp).Row
    'I want my VB routine to always replace AI1234 with "ID25" and I always want to replace AI123456 with "ID33"
    For j = 1 To i
        With Cells(j, 1)
            .Value = Replace(.Cells.Value, "AI123456", "ID33")
            .Value = Replace(.Cells.Value, "AI1234", "ID25")
        End With
    Next j
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,431
Messages
6,119,458
Members
448,899
Latest member
maplemeadows

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