Forum Discussion
Tommee
Jan 12, 2024Copper Contributor
Displaying a date using an excel vba macro
Hello. I've formatted a 4-digit numeric field 0 decimals which contains the year 2024 in cell E2. The user can change this value as needed. I am concatenating the month and the day using the DateSer...
NikolinoDE
Jan 12, 2024Gold Contributor
In your code, Curryear is assigned the value of cell E2, which is treated as a numeric value. To handle it as a year, you should use the Year function instead.
Dim Currmonth As Integer
Dim Currday As Integer
Dim Curryear As Integer
Curryear = Year(Range("E2").Value)
Currmonth = 4
Currday = 15
Range("C10:C10").Value = DateSerial(Curryear, Currmonth, Currday)
Currmonth = 6
Range("C11:C11").Value = DateSerial(Curryear, Currmonth, Currday)
Currmonth = 9
Range("C12:C12").Value = DateSerial(Curryear, Currmonth, Currday)
Currmonth = 1
Curryear = Curryear + 1
Range("C13:C13").Value = DateSerial(Curryear, Currmonth, Currday)
This code retrieves the year from cell E2 using the Year function and then uses it in the DateSerial function to create the desired dates.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- TommeeJan 12, 2024Copper ContributorThank you NikolinoDE!
I just copied your code as described.
Unfortunately, my year is displaying
as 1905 and the last entry as 1906.
I appreciate your help. From Tommee