Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default replace in multiple sheets


Hi, I am trying to find a specific text in a worksheet and then put a
certaint text in cells next to where first text is found. And I want to
do it in all worksheets in the file I have. Here is the code that I came
up with but this only works on the active worksheet that I am in... I
think it should work on all worksheets in the file. Does anyone know
why it would do that...

Sub macro2

Dim wks As Worksheet
Dim txtlookup As String
Dim txtchange As String

txtlookup = InputBox("Enter Text to Lookup:", "Enter Text")
txtchange = InputBox("Enter Text to change to:", "Enter Text")
For Each wks In ThisWorkbook.Sheets
For Each cell In Range("A1:A2000")
If cell.Value = txtlookup Then
cell.Offset(0, 1).Value = txtchange
End If
Next cell
Next wks
End Sub

Thanks, Jay


--
sa02000
------------------------------------------------------------------------
sa02000's Profile: http://www.excelforum.com/member.php...o&userid=27747
View this thread: http://www.excelforum.com/showthread...hreadid=568789

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default replace in multiple sheets

Range("A1:A2000")
will refer to the active sheet (if the code is in a general module).

So try:

For Each cell In wks.Range("A1:A2000")

But you may want to record a macro when you do Edit|Replace.

Then loop through all the sheets using that code.

sa02000 wrote:

Hi, I am trying to find a specific text in a worksheet and then put a
certaint text in cells next to where first text is found. And I want to
do it in all worksheets in the file I have. Here is the code that I came
up with but this only works on the active worksheet that I am in... I
think it should work on all worksheets in the file. Does anyone know
why it would do that...

Sub macro2

Dim wks As Worksheet
Dim txtlookup As String
Dim txtchange As String

txtlookup = InputBox("Enter Text to Lookup:", "Enter Text")
txtchange = InputBox("Enter Text to change to:", "Enter Text")
For Each wks In ThisWorkbook.Sheets
For Each cell In Range("A1:A2000")
If cell.Value = txtlookup Then
cell.Offset(0, 1).Value = txtchange
End If
Next cell
Next wks
End Sub

Thanks, Jay

--
sa02000
------------------------------------------------------------------------
sa02000's Profile: http://www.excelforum.com/member.php...o&userid=27747
View this thread: http://www.excelforum.com/showthread...hreadid=568789


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default replace in multiple sheets


Dave, Thanks. I will try your solution. Also, I found out that adding
the following line after worksheet FOR statement does the trick too.

wks.activate

which really does the same thing that you are saying.

Thanks Again.
Jay


--
sa02000
------------------------------------------------------------------------
sa02000's Profile: http://www.excelforum.com/member.php...o&userid=27747
View this thread: http://www.excelforum.com/showthread...hreadid=568789

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default replace in multiple sheets

Almost the same...

It actually activates that worksheet. wks.range(...) doesn't depend on the
selection of sheets.

And if the code is behind a worksheet, it still won't work correctly.

It's not usually a good idea to select sheets, select ranges or activate
workbooks. It just slows things down and makes it more difficult to debug the
code (my opinion).

sa02000 wrote:

Dave, Thanks. I will try your solution. Also, I found out that adding
the following line after worksheet FOR statement does the trick too.

wks.activate

which really does the same thing that you are saying.

Thanks Again.
Jay

--
sa02000
------------------------------------------------------------------------
sa02000's Profile: http://www.excelforum.com/member.php...o&userid=27747
View this thread: http://www.excelforum.com/showthread...hreadid=568789


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find replace in multiple sheets based on namebox gabitzu Excel Discussion (Misc queries) 2 December 19th 06 11:30 AM
Replace with Grouped Sheets Jim May Excel Discussion (Misc queries) 4 September 15th 06 02:19 PM
Multiple Sheets (Need to create 500 individual sheets in one workbook, pulling DATA Amaxwell Excel Worksheet Functions 4 August 17th 06 06:23 AM
how do I do a global replace in all sheets in a workbook? knutknut Excel Worksheet Functions 3 June 26th 06 04:57 PM
Find / Replace sheets vs workbook in VB Bony Pony Excel Worksheet Functions 0 December 8th 04 02:21 PM


All times are GMT +1. The time now is 02:45 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"