VBA Search & Replace Within Range

srdavisgb

Board Regular
Joined
Nov 5, 2011
Messages
51
I am attempting to search and replace a value within a specific range in a worksheet. I do not want to replace the value in other places in the worksheet. I've tried the following two vba options. In both cases the code is replacing the values in columns J & N with "1A". So, when the code attempts to find "1: " in column N, it cannot be found.

How can I search and replace "1: " only within the range specified (e.g., "J2:J" & lastrow)?

I do appreciate any suggestions.

Option #1

Dim r1 As Range
Set r1 = Range("J2:J" & lastrow)
r1.Replace What:="1: ", Replacement:="1A: "


Dim r2 As Range
Set r2 = Range("N2:N" & lastrow)
r2.Replace What:="1: ", Replacement:="1B: "

Option #2

Columns("J:J").Select
Selection.Replace What:="1: ", Replacement:="1A: "

Columns("N:N").Select
Selection.Replace What:="1: ", Replacement:="1B: "
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
How about
Code:
Range("J:J").Replace "1: ", "1A: ", xlPart, , , , False, False
Range("N:N").Replace "1: ", "1B: ", xlPart, , , , False, False
 
Upvote 0
This code (e.g., Range("J:J").Replace "1: ", "1A: ", xlPart, , , , False, False) do what I'd like it to do...

When "1: " is replaced in column J with "1A: ", it (e.g., "1: ") is also replaced in column N. So, the code Range("N:N").Replace "1: ", "1B: ", xlPart, , , , False, False cannot find "1: " in column N.

What am I missing?

Thanks for your help.
 
Upvote 0
Do you have formulae in column N that looks at column J?
Also do you have any merged cells?
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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