VBA replace character ONLY in range

lipanook

New Member
Joined
Nov 29, 2012
Messages
23
I ran into a little bug while running one of my macros this morning. After doing a bit of investigating I found that the problem was due to this line:

Code:
Range("b2:b" & Range("a1048576").End(xlUp).Row).Replace what:=" ", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False

The problem was due to the fact that earlier, I had been doing some 'non-macro' replacing through the find/replace dialog, and I had selected 'workbook' in the 'replace within' option. So when I later ran my code, it deleted all of the space characters in the entire workbook. Once I selected 'sheet' instead of 'workbook' the macro worked fine.

Once I give this macro to other users I don't want them to encounter the same problem, so I was wondering if anyone knew how to specify that i want to search only in the current sheet not the entire workbook. i thought that by searching within a range as my code should do, that it would not matter, but evidently it does.

Thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,217,441
Messages
6,136,654
Members
450,022
Latest member
Joel1122331

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