View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Need Help with interesting Find & Replace macro

Dim rng as Range, sh2 as worksheet, cell as Range
with workbooks("booka.xls").Worksheets("Sheet1")
set rng = .Range(.Cells(1,1),.Cells(1,1).End(xldown))
End with
set sh2 = Workbooks("Bookb.xls").Worksheets("sheet1")
for each cell in rng
sh.Cells.Replace What:=cell.Value, _
Replacement:=cell.offset(0,1).Value, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False
Next

If you want to count replacements, you will have to slowly loop through all
the cells in workbook B, writing the code to check it for values and make
the replacement if appropriate (recording the count).

--
Regards,
Tom Ogilvy

"sirdev" wrote in message
...
Hello everyone,

I am entirely new to macro programming in excel (excel 2000) and I need
to perform a very particular find and replace.

I have two workbooks (one sheet in each) we'll call them workbookA and
workbookB. The first workbook (workbookA) contains two columns the
first cell (A1) contains the search string I need for workbookB and the
cell beside A1 (B1) contains the replace string for workbookB.

Example:

workbookA_sheet1:

column_A column_B
find_string1 replace_string1
find_string2 replace_string2
find_string3 replace_string3


workbookB_sheet1:

any_column
find_string2
find_string3
find_string1

So for instance on the first pass it should search workbookB_sheet1 for
the string "find_string1" (from workbookA_sheet1) if found replace with
"replace_string1" (from workbookA_sheet1). Then use "find_string2" and
do the same thing until the end of workbookA_sheet1 has been reached.

Also, if possible I would like a count of the total number of
fields/cells replaced in workbookB_sheet1 (probably using a incremented
counter on the if statement for the search routine).

I hope all of this is clear. Any help is greatly appreciated. Thanks
in advance for everyones help.


Thanks,
Sriram