简体   繁体   English

获取从今天开始的月份和年份

[英]Get the month and year from today's date

I am trying to get the month and year for today's date.我正在尝试获取今天日期的月份和年份。

Sub automation()

Dim wsheet As Worksheet
Dim month As Integer
Dim year As Integer

Set wsheet = Application.Workbooks("try").Worksheets("try")

month = Application.WorksheetFunction.month(Date)

year = Application.WorksheetFunction.year(Date)

End Sub

My expected output is 5 for month and 2017 for year if today's date is 15/5/2017.如果今天的日期是 15/5/2017,我的预期输出是 5 个月和 2017 年。

You may have some problem because you've shadowed some existing functions Month and Year with your variable names month and year .您可能会遇到一些问题,因为您已经使用变量名monthyear隐藏了一些现有函数MonthYear So, use different variable names:所以,使用不同的变量名:

Dim m As Integer
Dim y As Integer

And then either:然后要么:

m = DatePart("m", Date)
y = DatePart("yyyy", Date)

Or:或者:

m = month(Date)
y = year(Date)

In my Excel 2010 (not tested in 2013) while Month is a worksheet function, it's not exposed to VBA for some reason.在我的 Excel 2010(未在 2013 年测试)中,虽然Month是一个工作表函数,但由于某种原因它没有暴露给 VBA。 If you want to use the WorksheetFunction instance of these, you can technically do it using the Application.Evaluate method, like so:如果您想使用这些WorksheetFunction实例,您可以使用Application.Evaluate方法在技术上做到这一点,如下所示:

m = Evaluate("MONTH(""" & Date & """)")
y = Evaluate("YEAR(""" & Date & """)")

The built-in VBA.DateTime.Month and VBA.DateTime.Year functions, however, are available and that is what would be used in the second example above.但是,内置的VBA.DateTime.MonthVBA.DateTime.Year函数是可用的,这将在上面的第二个示例中使用。

在此处输入图片说明

If you must for some reason retain the month and year variable names, then you need to fully qualify the function call to avoid error:如果由于某种原因必须保留monthyear变量名称,则需要完全限定函数调用以避免错误:

month = VBA.DateTime.Month(Date)
year = VBA.DateTime.Year(Date)

Change in your code like this:像这样更改您的代码:

Sub CurrentDate()

    Dim currentMonth As Long
    Dim currentYear As Long

    currentMonth = Month(Date)
    currentYear = Year(Date)

    Debug.Print currentMonth; currentYear

End Sub

Month and Year are functions of the VBA.DateTime , do not use them for variable names. MonthYearVBA.DateTime函数,不要将它们用于变量名。

In general, Application.WorksheetFunction does not have a function, related to current date, in contrast to VBA.DateTime.Month or VBA.DateTime.Year (or at least I did not find) any in the Excel Library.通常,与 Excel 库中的VBA.DateTime.MonthVBA.DateTime.Year (或至少我没有找到)相比, Application.WorksheetFunction没有与当前日期相关的函数。

dim this as date
this = Format(Date(), "yyyy") 
this = Format(Date(), "mm")

Answering late but I wanted to reference the VBA documentation from microsoft's own page.回答晚了,但我想参考微软自己页面上的 VBA 文档。

To get Month from date:从日期获取月份:

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/month-function https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/month-function

Sample snippet:示例片段:

Dim MyDate, MyMonth
MyDate = #February 12, 1969#    ' Assign a date.
MyMonth = Month(MyDate)    ' MyMonth contains 2.

To get Year from date:从日期获取年份:

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/year-function https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/year-function

Sample snippet:示例片段:

Dim MyDate, MyYear
MyDate = #February 12, 1969#    ' Assign a date.
MyYear = Year(MyDate)    ' MyYear contains 1969.

I like these answers, but I needed one that would contain both the month and year in the format (my use case is as an accountant).我喜欢这些答案,但我需要一个包含格式的月份和年份的答案(我的用例是一名会计师)。 This was my solution:这是我的解决方案:

Dim today As Date
today = Date ' Get the current date

Dim period As String
period = Format(today, "MM/YYYY") ' Convert the date to MM/YYYY 

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

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