Hello,
Being that no one had a solution for my issue I went and created the sheet the long way by creating the sheet from scratch instead of copying it. Sad to think all this could have been 10 lines intead of 50 but at least the program works. I will keep this page bookmarked if anyone has a better idea. Thanks
If you are interested in seeing what the code does checkout the following video I recorded: << unavailable video removed >>
'*************************************************************************************************************************************************************************'
'Proceeding to Create new sheet for user.
'Sht_TemplateVehicle.Copy After:=Sheets(Sheets.count)
'Code to recreate the Template vehicle sheet since I cant copy it
Str_ShtNewVehicleName = TxtBx_AddAccountColor & " " & TxtBx_AddAccountYear & " " & TxtBx_AddAccountMake & " " & TxtBx_AddAccountModel
Str_ShtNewVehicleCodeName = TxtBx_AddAccountColor & TxtBx_AddAccountYear & TxtBx_AddAccountMake & TxtBx_AddAccountModel
Arr_NewVehicleData = Array("Oil Change", "", Format(Date, "mm/dd/yyyy"), Replace(TxtBx_AddAccountMileage, ",", ""), Format(DateAdd("m", 6, Date), "mm/dd/yyyy"), _
Replace(TxtBx_AddAccountMileage, ",", "") + 5000, , "", 5000, 6)
Arr_NewVehicleHeaders = Array("Service Item", "Item Life Remaining (%)", "Service Date", "Service Mileage (Miles)", "Service Due (Date)", "Service Due (Miles)", _
"Service Notes", "Interval (Miles)", "Interval (Months)")
'1) Create and Sht Name and Code Name (DONE)
Sheets.Add(After:=Sheets(Sheets.Count)).Name = Str_ShtNewVehicleName
Set Sht_NewVehicleSht = Sheets(Sheets.Count)
ThisWorkbook.VBProject.VBComponents(Sht_NewVehicleSht.CodeName).Name = Str_ShtNewVehicleCodeName
'2) Black out sheet Cells (DONE)
Sht_NewVehicleSht.Cells.Interior.Color = Lng_BlackColor
'3) Create Table and Format (DONE)
Set LstObj_NewVehicle = Sht_NewVehicleSht.ListObjects.Add(SourceType:=xlSrcRange, Source:=Sht_NewVehicleSht.Range(Cells(2, 1), Cells(2, 9)))
With LstObj_NewVehicle
.Range.Interior.Color = xlNone
.Name = Replace(Sht_NewVehicleSht.Name, " ", "")
.TableStyle = "Vehicle_PM"
.HeaderRowRange = Arr_NewVehicleHeaders
.ListRows.Add: .ListRows(1).Range = Arr_NewVehicleData
.HeaderRowRange.Font.Size = 12
.ListColumns(4).DataBodyRange.NumberFormat = "#,##0"
.ListColumns(6).DataBodyRange.NumberFormat = "#,##0"
.ListColumns(9).DataBodyRange.NumberFormat = "#,##0"
.Range.HorizontalAlignment = xlCenter
.ListColumns(1).DataBodyRange.HorizontalAlignment = xlLeft
.ListColumns(7).DataBodyRange.HorizontalAlignment = xlLeft
.Range.BorderAround Weight:=xlMedium, Color:=Lng_WhiteColor
End With
Sht_NewVehicleSht.Columns.AutoFit
Sht_NewVehicleSht.Rows(1).RowHeight = 40
'4) Copy Code from Template Vehicle to new Sht (DONE)
'*** IF YOU ARE WONDERING WHY ALL THE COMMENTS, I SAVE THIS TEMPLATE AS A NOTEPAD FILE AND EDIT IT FOR MY WORKBOOKS WHEN I NEED IT. ***
'This code will break if you have not enabled Extensibility 5.3 | Go to tools > References > Microsoft Visual Basic for Applications Extensibility 5.3
Dim Code_SourceModule As VBIDE.CodeModule, Code_DestinationModule As VBIDE.CodeModule
Dim Int_ModCodeLines As Integer
Set Code_SourceModule = ActiveWorkbook.VBProject.VBComponents(Sht_TemplateVehicle.CodeName).CodeModule 'This is an existing sht so I added ".codename"
Set Code_DestinationModule = ActiveWorkbook.VBProject.VBComponents(Str_ShtNewVehicleCodeName).CodeModule 'This is a variable storing the codename as text so no ".codename" needed
Int_ModCodeLines = Code_SourceModule.CountOfLines
'The line below removes code in destination Sht if there is any. Cant paste of code exists in module.
If Code_DestinationModule.CountOfLines > 1 Then Code_DestinationModule.DeleteLines 1, Code_DestinationModule.CountOfLines
On Error Resume Next '*This is needed as the code breaks on the next line but works if you run it all the way through
Code_DestinationModule.AddFromString Code_SourceModule.Lines(1, Int_ModCodeLines)
'*************************************************************************************************************************************************************************'
For Each Sht In ThisWorkbook.Worksheets
If Not Sht Is Sht_Login Then Sht.Visible = xlVeryHidden
Next Sht