Thread: date format
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Patrick[_19_] Patrick[_19_] is offline
external usenet poster
 
Posts: 8
Default date format

On 2018-01-06 20:15:55 +0000, bill k said:

Is it possible to use a macro to have excel complete a date dd/mm with
the year depending on the month entered.
I.e. if I enter 27/01 it will return 27/01/2018 but if I enter 06/11
it will return 06/11/2017 or If the month is greater than 6 the year
will be yyyy -1.
Thanks Bill K


If you enter a date like 27/01, Excel will automatically complete the
date with the current year so the date will become 27/01/2018. Here's a
function that you can use :

Function NewDate(aDate As Date)
Dim theMonth As Integer
Dim theYear As Integer

theMonth = Month(aDate)
theYear = Year(aDate)

If theMonth 6 Then theYear = theYear - 1

NewDate = DateSerial(theYear, theMonth, Day(aDate))
End Function

You can easily turn this function in macro :

Sub NewDate(theDate as Range)
aDate = theDate.Value
[...]
theDate.Value = NewDate
End Sub