Formula Update Issues in a Do While Loop Excel 2007

nglenn82

New Member
Joined
Oct 31, 2014
Messages
4
I am trying to get a sheet reference name in a formula updated based on the previous sheet's name. I am trying to get this to work is a Do While Loop. Also within that loop, I am creating a set number of new sheets. Each new sheet should reference the previous sheet. Attached is the macro that I am working with. Currently, when I run this, it creates an error because it places single tick marks (') around the number that appears after the dash in the sheet name (which is the week). Any help that somebody can provide is greatly appreciated. Thanks in advance.

Code:
Sub CREATE_Q1()
    Dim X As Integer
    Dim I As Integer

    I = 13

    Do While X < I
        ActiveSheet.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = "Q1 - " & X + 1
        Cells.Replace What:=Sheets(Sheets.Count - 2).Name & "!", Replacement:=Sheets(Sheets.Count - 1).Name & "!", _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        X = X + 1
    Loop
    Sheets("NOTES").Visible = False
    Sheets("Q1 - 1").Select
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Didn't help. After I run the macro, the issue is that it places the single tick marks around the last number of the sheet name in the formula

Code:
=IFERROR(VLOOKUP($C53,Q1 - '1'!$A$34:$M$53,4,FALSE),"")

That is the formula that I am trying to auto update via the above macro. As you can see, the single tick marks are getting placed around the 1, which causes it to not run correctly.

The way I would need the formula to look would be

Code:
=IFERROR(VLOOKUP($C53,'Q1 - 1'!$A$34:$M$53,4,FALSE),"")

With the tick marks wrapped around the entire sheet name. My guess is that it has to do with the replace section.

Code:
Cells.Replace What:=Sheets(Sheets.Count - 2).Name & "!", Replacement:=Sheets(Sheets.Count - 1).Name & "!"

I'm just not sure though on how to right it so that the tick marks wrap correctly.
 
Last edited:
Upvote 0
Yes. The Do While Loop creates new sheets that are a copy of a Template. As it creates those sheets, it renames them accordingly; Q1 - 1 (For Quarter 1, Week 1). Each sheet has multiple formulas in it that need to reference the previous sheet. So for instance, with the listed above code, The Q1 - 2 sheet formulas should reference Q1 - 1 and so on.
 
Upvote 0
I see.It 's because of the naming of the sheet names. Excel is thinking Q1 is a cell ref not a sheet.
For a quick fix, if you make sure that the sheet you copy and the one prior to that have a space in their name then the below will work
Change the find/replace to
Code:
Cells.Replace What:="'" & Sheets(Sheets.Count - 2).Name & "'", Replacement:="'" & Sheets(Sheets.Count - 1).Name & "'"
 
Upvote 0
This helped with the tick marks, but now I am running into another problem of not referenceing the correct Sheet. Now, when it copies, it just repeats the Q1 - 01 sheet without updating to the actual previous sheet. Here is updated macro

Code:
Sub CREATE_Q1()
    Dim WKS As Integer
    Dim QTR As Integer
    Dim CurSheet As String
    QTR = 13
    Sheets("NOTES").Visible = True
    Do While WKS < QTR
        WKS = WKS + 1
        ActiveSheet.Copy After:=Sheets(Sheets.Count)
        ActiveSheet.Name = Application.WorksheetFunction.VLookup(WKS, Range("$P$2:$R$15"), 2, False)
        Cells.Replace _
        What:=Sheets(Sheets.Count - 2).Name & "!", _
        Replacement:="'" & Application.WorksheetFunction.VLookup(WKS, Range("$P$2:$R$15"), 3, False) & "'!", _
        LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Loop
    Sheets("NOTES").Visible = False
    Sheets("Q1 - 01").Select
End Sub

I've also created a small table within the sheet for referencing so that it will now use a vlookup to find the correct sheet names and which sheet it should reference. That table looks like this:


WEEKSHEETSREFERENCE
1Q1 - 01TEMPLATE
2Q1 - 02Q1 - 01
3Q1 - 03Q1 - 02
4Q1 - 04Q1 - 03
5Q1 - 05Q1 - 04
6Q1 - 06Q1 - 05
7Q1 - 07Q1 - 06
8Q1 - 08Q1 - 07
9Q1 - 09Q1 - 08
10Q1 - 10Q1 - 09
11Q1 - 11Q1 - 10
12Q1 - 12Q1 - 11
13Q1 - 13Q1 - 12

<tbody>
</tbody><colgroup><col span="2"><col></colgroup>
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

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