View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Per Jessen Per Jessen is offline
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