VBA Search Workbook Question (Excel 2007)

F

Francis

I have been using this particular macro for 12 years, including
several months since I got Excel 2007 on my work computer. But
something happened today that had me puzzled till I stepped through
the macro and pinpointed the problem.

My macro (snippet below) removes dashes and blanks from column B.

Sub ReplaceStuff()
Columns("B:B").Select
Selection.Replace What:="-", Replacement:="", LookAt:=xlPart
Selection.Replace What:=" ", Replacement:="", LookAt:=xlPart
End Sub

Today, when I ran the macro, I discovered that it removed the dashes
and blanks from the entire sheet (only a one-sheet workbook) instead
of just the Selection. Then I remembered that a while back I was
trying to create a macro to search an entire workbook but the code
generated from the macro recorder didn't seem to have anything to
specify the Sheet or the Workbook. Also, I had done a search earlier
today and manually change the Within option to Workbook. Just as I
suspected, when I manually changed the Within option back to Sheet, my
macro functioned properly.

I have two questions. Maybe they have the same answer.

Is there a VBA way to restrict a search to the Selection regardless
what the last choice was in the Find dialog?

Is there a VBA way to search an entire workbook regardless what the
last choice was in the Find dialog?

Thanks,
Francis (TheExcelAddict.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top