Sub test()
' This is an Excel VBA code designed to automatically update the workbook
' and display the most recent year and 2 years prior for trainings
' This code will automatically run upon opening the workbook
' This code will update the years after December 31st 20XX, therefore
' for each new year, information from the 3rd previous year
' should be recorded for future use so as not to lose any
' training histories
'------------------------------------------------------------------------
' Set initial year values for overall spreadsheet in first 3 columns
Dim Y1 As Variant
Y1 = 2018
Range("c10").Value = Y1
Range("d10").Value = Y1 + 1
Range("e10").Value = Y1 + 2
' Set current date to display in mm/dd/yyyy format
Dim currentdate As Date
currentdate = Date
Range("h10").Value = currentdate
' Set initial end of year date to display in mm/dd/yyyy format
Dim endofyear As Date
endofyear = CDate("12/31/2020")
Range("i10").Value = endofyear
Dim endofyear2 As Date
endofyear2 = DateSerial(2020, 12, 31)
Range("f15").Value = endofyear2
' This portion of the code is an if statement to determine whether or not
' the current date has past the end of year date
' If it has, the initial year values will all update by 1 or 2 year(s), if
' not they will remain the same
If currentdate < endofyear2 Then
Range("c10").Value = Range("c10").Value
Range("d10").Value = Range("c10").Value + 1
Range("e10").Value = Range("c10").Value + 2
Else:
Range("c10").Value = Range("c10").Value + 1
Range("d10").Value = Range("c10").Value + 1
Range("e10").Value = Range("c10").Value + 2
End If
End Sub
Bookmarks