简体   繁体   中英

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.

You may have some problem because you've shadowed some existing functions Month and Year with your variable names month and year . 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. If you want to use the WorksheetFunction instance of these, you can technically do it using the Application.Evaluate method, like so:

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.

在此处输入图片说明

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:

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.

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.

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.

To get Month from date:

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

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 

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