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

Can anyone tell me why my code below only deletes data from worksheet April
and not all of the selected worksheets? I intend that the TextBox1 value be
found on all selected worksheets and be replaced by nothing, in effect
removing the data from all worksheets.



Private Sub CommandButton1_Click()

x = TextBox1.Value

If ActiveSheet.Name = "April" Then

Sheets(Array("April", "May", "June", "July", "August",
"September")).Select

Cells.Replace What:=x, Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Sheets("April").Select

End If

Unload UserForm1

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,533
Default Search and replace problem

Hi

Replace only work on the active sheet, or while using a sheet reference.
This should do whay you want:

Private Sub CommandButton1_Click()
Dim ShArr
Set ShArr = Sheets(Array("April", "May", "June", "July", "August",
"September"))
x = TextBox1.Value

If ActiveSheet.Name = "April" Then
For Each sh In ShArr
With sh
.Cells.Replace What:=x, Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
End With
Next
End If

Unload UserForm1
End Sub

Regards,
Per

"ordnance1" skrev i meddelelsen
...
Can anyone tell me why my code below only deletes data from worksheet
April and not all of the selected worksheets? I intend that the TextBox1
value be found on all selected worksheets and be replaced by nothing, in
effect removing the data from all worksheets.



Private Sub CommandButton1_Click()

x = TextBox1.Value

If ActiveSheet.Name = "April" Then

Sheets(Array("April", "May", "June", "July", "August",
"September")).Select

Cells.Replace What:=x, Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Sheets("April").Select

End If

Unload UserForm1

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default Search and replace problem

"ordnance1" je napisao u poruci interesnoj
...
Can anyone tell me why my code below only deletes data from worksheet
April
Cells.Replace What:=x, Replacement:="", LookAt:=xlWhole, _


your 'cells' method is referring to 'active' not 'selected', so that is
allways 'april'
maybe to try:

----
for each ws in Sheets(Array("April", "May", "June", "July", "August",
"September"))
ws.Cells.Replace What:=x, Replacement:="", LookAt:=xlWhole, _

next
----


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Search and replace problem


there are too many things wrong in VBA to attempt an explanation when
VBA doesn't work the way you expect. In this case, it is just better to
use good programming style and change the code as follows

Private Sub CommandButton1_Click()

ShtNames = Array("April","June","July","August","September")

x = TextBox1.Value

for each shtname in ShtNames
Set Sht = sheets(shtname)

Sht.Cells.Replace What:=x, Replacement:="", _
LookAt:=xlWhole, _
ReplaceFormat:=False
next sht

Unload UserForm1

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: http://www.thecodecage.com/forumz/member.php?u=229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=196113

http://www.thecodecage.com/forumz

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 91
Default Search and replace problem

On Apr 16, 3:35*pm, joel wrote:
there are too many things wrong in VBA to attempt an explanation when
VBA doesn't work the way you expect. *In this case, it is just better to
use good programming style and change the code as follows

Private Sub CommandButton1_Click()

ShtNames = Array("April","June","July","August","September")

x = TextBox1.Value

for each shtname in ShtNames
Set Sht = sheets(shtname)

Sht.Cells.Replace What:=x, Replacement:="", _
LookAt:=xlWhole, _
ReplaceFormat:=False
next sht

Unload UserForm1

End Sub

--
joel
------------------------------------------------------------------------
joel's Profile:http://www.thecodecage.com/forumz/member.php?u=229
View this thread:http://www.thecodecage.com/forumz/sh...d.php?t=196113

http://www.thecodecage.com/forumz



One argument is there in Replace method of Range
Searchwithin:=xlsearchwithinworkbook.
If you add this then the replacement will be for entire workbook.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default Search and replace problem

Thanks or your reply.

I have to avoid replacing through the entire workbook. This is a vacation
calendar which combines 3 workgroups and for record keeping I can not remove
a name if the employee took vacation in prior months (even if the employee
is now gone). I now have a series of If statements that seems to do the
trick.

If ActiveSheet.Name = "March" Then

Set ShArr = Sheets(Array("March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"))
x = TextBox1.Value

For Each Sh In ShArr
With Sh
.Cells.Replace What:=x, Replacement:="", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False

Range("A3").Activate
End With
Next
Sheets("March").Select
End If



"Javed" wrote in message
...
On Apr 16, 3:35 pm, joel wrote:
there are too many things wrong in VBA to attempt an explanation when
VBA doesn't work the way you expect. In this case, it is just better to
use good programming style and change the code as follows

Private Sub CommandButton1_Click()

ShtNames = Array("April","June","July","August","September")

x = TextBox1.Value

for each shtname in ShtNames
Set Sht = sheets(shtname)

Sht.Cells.Replace What:=x, Replacement:="", _
LookAt:=xlWhole, _
ReplaceFormat:=False
next sht

Unload UserForm1

End Sub

--
joel
------------------------------------------------------------------------
joel's Profile:http://www.thecodecage.com/forumz/member.php?u=229
View this
thread:http://www.thecodecage.com/forumz/sh...d.php?t=196113

http://www.thecodecage.com/forumz



One argument is there in Replace method of Range
Searchwithin:=xlsearchwithinworkbook.
If you add this then the replacement will be for entire workbook.


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
search and replace Timo Excel Discussion (Misc queries) 1 August 25th 09 06:06 PM
Search and replace Rockbear Excel Worksheet Functions 0 October 13th 08 08:25 AM
Search and Replace Problem John Excel Programming 3 September 21st 08 06:10 PM
Problem with search and replace data,thanks for you help in advance. yoyo2000 Excel Discussion (Misc queries) 1 June 20th 06 03:56 AM
Search and Replace Rebecca New Users to Excel 2 June 5th 06 06:42 AM


All times are GMT +1. The time now is 10:05 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"