Want2BExcel
Board Regular
- Joined
- Nov 24, 2021
- Messages
- 114
- Office Version
- 2016
- Platform
- Windows
Hi everybody,
I have this workbook where my template sheet can be copied and renamed with a buttonclick (The full code is posted in the bottom).
My problem is in the end of the full code. It's this part just below this text. In this example it creates "JAN.24" and therefor I have to change the formula in the cells below to get data from the previous sheet "DEC.23". This work fine, but....
I'm trying to find a way to avoid using "DEC.23" and instead use something dynamic. Because next sheet I create will be "FEB.24" which need "JAN.24" in the formula and so forth
New sheets will always be inserted to the left of "DataSheet"!
The full code:
I have this workbook where my template sheet can be copied and renamed with a buttonclick (The full code is posted in the bottom).
My problem is in the end of the full code. It's this part just below this text. In this example it creates "JAN.24" and therefor I have to change the formula in the cells below to get data from the previous sheet "DEC.23". This work fine, but....
I'm trying to find a way to avoid using "DEC.23" and instead use something dynamic. Because next sheet I create will be "FEB.24" which need "JAN.24" in the formula and so forth
VBA Code:
With ActiveSheet
wsName = Sheets("DEC.23").Name
Range("AS8").Formula = "=" & wsName & "!AX$8 "
Range("AS9").Formula = "=" & wsName & "!AX$9"
Range("AS10").Formula = "=" & wsName & "!AX$10"
Range("AS11").Formula = "=" & wsName & "!AX$11"
Range("AS12").Formula = "=" & wsName & "!AX$12"
Range("AS13").Formula = "=" & wsName & "!AX$13"
Range("AS14").Formula = "=" & wsName & "!AX$14"
Range("AS15").Formula = "=" & wsName & "!AX$15"
Range("AS16").Formula = "=" & wsName & "!AX$16"
Range("AS17").Formula = "=" & wsName & "!AX$17"
Range("AS18").Formula = "=" & wsName & "!AX$18"
Range("AS19").Formula = "=" & wsName & "!AX$19"
Range("AS20").Formula = "=" & wsName & "!AX$20"
Range("AS21").Formula = "=" & wsName & "!AX$21"
End With
New sheets will always be inserted to the left of "DataSheet"!
The full code:
VBA Code:
Sub SkiftTIDSINFOformula()
Dim sh As Worksheet
Dim answer As Integer
s = [ScMonth]
y = [ScYear]
answer = msgbox("BEMÆRK! Du vil ikke kunne ændre måned på det nye ark" & vbCrLf & "Er måned og år, som du ønsker?" & vbCrLf & "Er du sikker på du ønsker at forsætte?", vbQuestion + vbYesNo, "Opret nyt månedsark")
If answer = vbYes Then
Else: Exit Sub
End If
On Error Resume Next
'check sheet exists already
Set sh = Sheets(s & y)
On Error GoTo 0
If Not sh Is Nothing Then msgbox "Den måned findes allerede. Prøv igen", vbCritical: Exit Sub
'copy your template before that sheet
Sheets("Template").Copy before:=Sheets("DataSheet")
With ActiveSheet
'rename sheet
.Name = Left(s, 3) & "." & Right(y, 2)
ActiveSheet.Unprotect
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Select
Selection.Delete
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 3")).Select
Selection.Delete
Range("B1:F1").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("F6:AP7").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B1:C1").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
Range("I1:N1").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
With ActiveSheet
wsName = Sheets("DEC.23").Name
Range("AS8").Formula = "=" & wsName & "!AX$8 "
Range("AS9").Formula = "=" & wsName & "!AX$9"
Range("AS10").Formula = "=" & wsName & "!AX$10"
Range("AS11").Formula = "=" & wsName & "!AX$11"
Range("AS12").Formula = "=" & wsName & "!AX$12"
Range("AS13").Formula = "=" & wsName & "!AX$13"
Range("AS14").Formula = "=" & wsName & "!AX$14"
Range("AS15").Formula = "=" & wsName & "!AX$15"
Range("AS16").Formula = "=" & wsName & "!AX$16"
Range("AS17").Formula = "=" & wsName & "!AX$17"
Range("AS18").Formula = "=" & wsName & "!AX$18"
Range("AS19").Formula = "=" & wsName & "!AX$19"
Range("AS20").Formula = "=" & wsName & "!AX$20"
Range("AS21").Formula = "=" & wsName & "!AX$21"
End With
Range("AS8").Select
' ActiveSheet.Protect
'activate template sheet
Worksheets("Template").Activate
End With
End Sub