Update Formulas on Consecutive Sheets in a Range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I add sheets, I have copy a base sheet (P1) and paste it to a new sheet.
I have a macro that copies the row and column properties and then applies it
to the new sheets.
What I want to do now is run a macro to change the formulas. A simple
replace command works, but I need code that will advance by one for each new
sheet. I recorded the macro below to give you a feel of what I am trying to
do. I have sheets P1 to P30, but could have more P? sheets, up to 100. All
the sheets will have the same formulas, just 1 more than the previous sheet.
You can assume always running the macro from the P1 sheet. The initial
formula always starts with column 17 and each each thereafter must be +1,
then +2, etc.
Thanks in advance!

Sub UpdateFormulas()
Sheets("P3").Select
Range("C15:I15").Select
Selection.Replace What:="17", Replacement:="19", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("C24:I24").Select
Selection.Replace What:="17", Replacement:="19", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("P4").Select
Range("C15:I15").Select
Selection.Replace What:="17", Replacement:="20", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("C24:I24").Select
Selection.Replace What:="17", Replacement:="20", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Sheets("P5").Select
End Sub
 
David, I haven't tried this but:
Sub UpdateFormulas()
Dim iSheet%, sSheet$, sNum$
For iSheet = 2 To 100
sSheet = Format$(iSheet, "###")
sNum = Format$(iSheet+16, "###")
On Error Goto UFZ1
Sheets(sSheet).Select
On Error Goto 0
Range("C15:I15").Select
Selection.Replace What:="17", Replacement:=sNum, LookAt:=xlPart, _
etc.
Next iSheet
UFZ1:
End Sub ' David2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top