简体   繁体   English

在 Excel VBA 中,尝试将输入的月份数转换为完整日期

[英]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).您好,我正在尝试将输入的月份数转换为包含当年该月最后一天的完整日期(mm/dd/yyyy)。 For example if the user were to enter "01" I would need to store the date "01/31/2021".例如,如果用户要输入“01”,我需要存储日期“01/31/2021”。 If they entered "11" it would be "11/30/2021".如果他们输入“11”,那将是“2021 年 11 月 30 日”。 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.因此,如果今天的日期在该月的前 9 天内,则默认为上个月的最后一天,如果超过该月的前 9 天,则默认为上个月的最后一天。 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如果用户不必知道和输入他们需要的哪个月的最后一天,例如“09/30/2021”将其存储在 stopDate 变量中,他们只需输入“09”并输入“09/ 30/2021" 存储在 stopDate 变量中

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.如果我了解您在做什么,如果今天的日期>9,这应该返回上个月的最后一天,否则它将返回前第二个月的最后一天。

The last day of the preceding month is obtained by using 0 for the Day argument in the Dateserial function. Dateserial function 中的Day参数使用0获得上个月的最后一天。

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感谢 Ron Rosenfield,我能够解决问题并完成所需的 DateSerial function 和 1 行代码

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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