Hi Excel VBA Experts,
I have a few Subs that does what it's supposed to - it takes an Excel Template, makes a copy and populates it with data from multiple Tabs.
My objective is to make a copy of the Template ("Master Data Roll Up - Template.xlsx") to "Master Data Roll Up - Template 02.11.2022.xlsx.(by adding today's date).
I declared a global variable((WBkNameRollUp).
How do I make a copy of the template and add today's date to the new file name? Then, refer to the new file as "Template Name" + "Today's date"?
I'm not a VBA programmer . I would usually adapt VBA codes to suit my needs. Can someone please assist me with this?
Option Explicit
Dim WBkName As String
'Const WBkName As String = "Master Data Calculator.xlsm"
Const WBkNameRollUp As String = "Master Data Roll Up - Template.xlsx"
Sub SaveToRelativePath()
Dim relativePath As String
Workbooks.Open ("C:\PowerBI Hardrive\Master Data Roll Up - Template.xlsx")
relativePath = ThisWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
ActiveWorkbook.SaveAs filename:=relativePath
End Sub
Sub PIT_1_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 1 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Application.WindowState = xlNormal
Windows(WBkNameRollUp).Activate
Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
Sub PIT_2_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 2 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_3_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 3 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_4_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 4 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_6_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 6 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_1_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 1 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_2_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 2 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_3_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 3 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_4_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 4 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_6_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 6 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub CopyFormulaDown()
Dim LastPopulatedRow As Long
Windows(WBkNameRollUp).Activate
'Find last populated row
LastPopulatedRow = Range("S" & Rows.Count).End(xlUp).Row
'Select the rows where formula is to be populated
Range("U2: " & "V" & LastPopulatedRow).FillDown
Windows(WBkNameRollUp).Activate
Sheets("PVT").Activate
ActiveWorkbook.RefreshAll
ActiveWindow.WindowState = xlMaximized
Range("F4").Font.Bold = True
Range("F4").Interior.ColorIndex = 28
ActiveWindow.Zoom = 100
End Sub
Sub ColorNAs()
Sheets("Data").Activate
On Error Resume Next
ActiveSheet.UsedRange.SpecialCells(xlConstants, xlErrors).Interior.Color = 255
On Error GoTo 0
End Sub
Sub cMasterALLRollUpCopyNPaste()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Call SaveToRelativePath
Call PIT_1_USA
Call PIT_1_USA
Call PIT_2_USA
Call PIT_3_USA
Call PIT_4_USA
Call PIT_6_USA
Call PIT_1_INTL
Call PIT_2_INTL
Call PIT_3_INTL
Call PIT_4_INTL
Call PIT_6_INTL
Call CopyFormulaDown
Call ColorNAs
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
MsgBox "10 PITs data copied to Master Roll-Up Template" & vbCrLf & _
"All PVT Refreshed"
End Sub
I have a few Subs that does what it's supposed to - it takes an Excel Template, makes a copy and populates it with data from multiple Tabs.
My objective is to make a copy of the Template ("Master Data Roll Up - Template.xlsx") to "Master Data Roll Up - Template 02.11.2022.xlsx.(by adding today's date).
I declared a global variable((WBkNameRollUp).
How do I make a copy of the template and add today's date to the new file name? Then, refer to the new file as "Template Name" + "Today's date"?
I'm not a VBA programmer . I would usually adapt VBA codes to suit my needs. Can someone please assist me with this?
Option Explicit
Dim WBkName As String
'Const WBkName As String = "Master Data Calculator.xlsm"
Const WBkNameRollUp As String = "Master Data Roll Up - Template.xlsx"
Sub SaveToRelativePath()
Dim relativePath As String
Workbooks.Open ("C:\PowerBI Hardrive\Master Data Roll Up - Template.xlsx")
relativePath = ThisWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
ActiveWorkbook.SaveAs filename:=relativePath
End Sub
Sub PIT_1_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 1 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Application.WindowState = xlNormal
Windows(WBkNameRollUp).Activate
Worksheets("Data").Range("A2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
Sub PIT_2_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 2 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_3_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 3 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_4_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 4 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_6_USA()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 6 - US").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_1_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 1 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_2_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 2 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_3_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 3 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_4_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 4 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub PIT_6_INTL()
Windows(ThisWorkbook.Name).Activate
Sheets("PIT 6 - INTL").Activate
Range("A2").Select
Selection.End(xlToRight).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Windows(WBkNameRollUp).Activate
Worksheets("Data").Activate
Range("A1").Select
Range("A1").End(xlDown).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(ThisWorkbook.Name).Activate
End Sub
Sub CopyFormulaDown()
Dim LastPopulatedRow As Long
Windows(WBkNameRollUp).Activate
'Find last populated row
LastPopulatedRow = Range("S" & Rows.Count).End(xlUp).Row
'Select the rows where formula is to be populated
Range("U2: " & "V" & LastPopulatedRow).FillDown
Windows(WBkNameRollUp).Activate
Sheets("PVT").Activate
ActiveWorkbook.RefreshAll
ActiveWindow.WindowState = xlMaximized
Range("F4").Font.Bold = True
Range("F4").Interior.ColorIndex = 28
ActiveWindow.Zoom = 100
End Sub
Sub ColorNAs()
Sheets("Data").Activate
On Error Resume Next
ActiveSheet.UsedRange.SpecialCells(xlConstants, xlErrors).Interior.Color = 255
On Error GoTo 0
End Sub
Sub cMasterALLRollUpCopyNPaste()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Call SaveToRelativePath
Call PIT_1_USA
Call PIT_1_USA
Call PIT_2_USA
Call PIT_3_USA
Call PIT_4_USA
Call PIT_6_USA
Call PIT_1_INTL
Call PIT_2_INTL
Call PIT_3_INTL
Call PIT_4_INTL
Call PIT_6_INTL
Call CopyFormulaDown
Call ColorNAs
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
MsgBox "10 PITs data copied to Master Roll-Up Template" & vbCrLf & _
"All PVT Refreshed"
End Sub