简体   繁体   中英

In Excel VBA, trying to convert an inputted month number to a full date

Hello I'm trying to converted an inputted month number to a full date with the last day of that month for the current year (mm/dd/yyyy). For example if the user were to enter "01" I would need to store the date "01/31/2021". If they entered "11" it would be "11/30/2021". I feel like I'm just having a brain fart and that it should be something simple but I can't figure it out.

As of right now I am using this code

Dim stopDate, defaultDate As String

If (Day(Now()) < 10) Then
    defaultDate = Format(WorksheetFunction.EoMonth(Now(), -2), "mm/dd/yyyy")
Else
    defaultDate = Format(WorksheetFunction.EoMonth(Now(), -1), "mm/dd/yyyy")
End If
stopDate = InputBox("Input the last day of the last month (mm/dd/yyyy) of the 12 month period.", "User date", defaultDate)

So if today's date is within the first 9 days of the month, it will default to the last day of the month before last, and if it's past the first 9 days of the month, it will default to the last day of last month. And this default date is what we need the majority of the time, but if the default day needs to be changed, then the user has to enter a new month and the new last day of that month which I would like to avoid the hassle. It would be easier if instead of the user having to know and input the last day of which month they need like "09/30/2021" to have it stored in the stopDate variable they could just input "09" and have "09/30/2021" stored in the stopDate variable

Try:

Function someDate(mnth As Long) As Date
    someDate = DateSerial(Year(Date), _
        mnth - IIf(Day(Date) > 9, 0, 1), 0)
End Function

If I understand what you are doing, this should return the last day of the preceding month if todays date is >9, else it will return the last day of the 2nd preceding month.

The last day of the preceding month is obtained by using 0 for the Day argument in the Dateserial function.

Thanks to Ron Rosenfield I was able to solve the problem and accomplish what was needed with the DateSerial function and 1 additional line of code

Dim month As Long
Dim stopDate, defaultMonth As String

If (Day(Now()) < 10) Then
        defaultMonth = Format(WorksheetFunction.EoMonth(Now(), -2), "mm")
    Else
        defaultMonth = Format(WorksheetFunction.EoMonth(Now(), -1), "mm")
    End If
    month = InputBox("Input the last month (mm) of the 12 month period there is data for.", "User date", defaultMonth)
    stopDate = DateSerial(Year(Date), month + 1, 0)

This can be reduced - and dimensioning lacks a little - so:

Dim month           As Integer
Dim stopDate        As Date 
Dim defaultMonth    As String

defaultMonth = Format(DateAdd("m", -1, DateAdd("d", -9, Date)), "mm")
month = Val(InputBox("Input the last month (mm) of the 12 month period there is data for.", "User date", defaultMonth))
stopDate = DateSerial(Year(Date), month + 1, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM