Find and Replace for several sheets

ddnron

Board Regular
Joined
Oct 11, 2003
Messages
115
Hi, Thanks in advance for your help.

This works fine for "Overview" worksheet.

I need this to run for two sheets, at the same time.

The target cells are different, on the sheets. "Overview" worksheet target cell is B2. For "Deposits" worksheet target cell is D7.

Need this to run for a worksheet called "deposits".... right after it runs for "Overview" worksheet.

Would appreciate! Thanks




Private Sub CommandButton3_Click()

Dim userResponse As Integer
userResponse = MsgBox("Use this only ONCE...at beginning of the year, to change the dates. Do you want to proceed?", vbYesNo)

If userResponse = vbYes Then

Sheets("Overview").Unprotect
Sheets("Deposits").Unprotect

Sheets("Overview").Select
Range("b2").Select

Cells.Replace What:=Application.InputBox("Year to search:", "Enter Year from Date in Left Corner", , , , , 2), Replacement:=Application.InputBox("Enter New Year", "Update to New Year", , , , , 2), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

ElseIf userResponse = vbNo Then
Else

End If

Sheets("Overview").Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False
Sheets("Deposits").Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Add this following the "Dim UserResponse" line
Code:
Dim shArr(1 To 2, 1 To 2), i As Long
shArr(1, 1) = "Overview"
shArr(1, 2) = "B2"
shArr(2, 1) = "Deposits"
shArr(2, 2) = "D7"

Add this following the "If userResponse = vbYes Then"
Code:
For i = LBound(shArr) To UBound(shArr)
    With Sheets(shArr(i, 1))
        .Unprotect
            .Range(shArr(i, 2)).Replace What:= etc etc
        .Protect DrawingObjects:=False, etc etc
    End With
Next i
End If
Leave the ElseIf and Else lines out.


Please use code tags. Makes things so much easier to read/copy.
 

Attachments

  • Use Code Tags MrExcel.JPG
    Use Code Tags MrExcel.JPG
    50.2 KB · Views: 2
Upvote 0
Solution
Add this following the "Dim UserResponse" line
Code:
Dim shArr(1 To 2, 1 To 2), i As Long
shArr(1, 1) = "Overview"
shArr(1, 2) = "B2"
shArr(2, 1) = "Deposits"
shArr(2, 2) = "D7"

Add this following the "If userResponse = vbYes Then"
Code:
For i = LBound(shArr) To UBound(shArr)
    With Sheets(shArr(i, 1))
        .Unprotect
            .Range(shArr(i, 2)).Replace What:= etc etc
        .Protect DrawingObjects:=False, etc etc
    End With
Next i
End If
Leave the ElseIf and Else lines out.


Please use code tags. Makes things so much easier to read/copy.

Thanks Jolivanes - works! - and thanks for sharing your knowledge about code Tags!
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,132
Members
448,947
Latest member
test111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top