I writing a VBA code that runs every time a new sheet is added to a workbook and I have hit a snag. This is what I have so far...
Sheet 1 (Job2Date) is a summary of all the sheets that follow. The template already has Week (1) showing. Every sheet that is added the number changes on the sheet name (Week (2), Week (3), Week (4), etc...) There is a hidden sheet that is the template for all new sheets that copies and pastes to the new sheet. Then aA pop up comes up and asks what the first day of that week is and fills in the appropriate sections. In cell "A442" it pastes the sheet name. On the main sheet (Job2Date) it adds new week total columns to the first empty column and fills in the correct dates and week name.
Now the part that I am stuck on is I need to replace all the formulas in the new section with the new sheet name.
Formula: =IF(Week!$G6="","",Week!$G6)
I have this code that works the first time, but the Sheet names are always changing. I need it to replace "Week!" with whatever the new sheet name ("A442" of the new sheet) is and I need it to replace only in the 4 columns that were just added.
Sub Replace()
Sheets("Job2Date").Select
Range("W12:Z701").Select
Sheets("Week (2)").Select
Range("A442").Select
Selection.Copy
Sheets("Job2Date").Select
Selection.Replace What:="Week!", Replacement:="'Week (2)'!", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFor`enter code here`mula2
End Sub
This is my entire code so far.
Private Sub Workbook_NewSheet(ByVal Sh As Object)
Sheets("Job2Date").Select
Sheets("Week").Visible = True
Sheets("Week").Select
Sheets("Week").Copy After:=Worksheets(Worksheets.Count)
Sheets("Week").Select
ActiveWindow.SelectedSheets.Visible = False
Call BlankWorksheets 'This deletes any blank sheets in the workbook
Sheets(Sheets.Count).Select
Dim myValue As Variant
'Knowing the first date of the week
myValue = InputBox("What is the start date of this week?", dd, mm, yyyy)
Range("O2").Value = myValue
'Finding new tab name
ActiveSheet.[a442] = ActiveSheet.Name
'Adding a new week onto Job2Date
Select Case Sheets("Job2Date").Range("A1") = ""
Case True
Sheets("Job2Date").Range("O7:R701").Copy Sheets("Job2Date").Range("A7")
Case False
Sheets("Job2Date").Range("O7:R701").Copy Sheets("Job2Date").Range("XFD7").End(xlToLeft).Offset(0, 1)
End Select
'Name of New tab added Job2Date
ActiveSheet.Range("A442").Copy
Sheets("Job2Date").Select
Sheets("Job2Date").Range("XFD9").End(xlToLeft).Offset(, 1).PasteSpecial xlValues
Application.CutCopyMode = False
ActiveCell.Resize(1, 4).Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
'Adding End Date to Job2Date
Call GoToLast 'This goes to the last sheet that was active
ActiveSheet.Range("AM2").Copy
Sheets("Job2Date").Select
Sheets("Job2Date").Range("XFD10").End(xlToLeft).Offset(, 1).PasteSpecial xlValues
Application.CutCopyMode = False
ActiveCell.Resize(1, 2).Merge
'Go to the Previous Sheet
Call GoToLast
Range("A1").Select
End Sub