Solved

# VBA Excel Date Calculation

Posted on 2010-11-25
459 Views
Hi,

I am looking for a way to give me the total number of months between 2 dates, for eg. "2012/03/30 - 2010/09/30"

Please can someone give me the formula that will produce the result of : 18
0
Question by:NerishaB
• 4
• 3
• 2
• +2

LVL 1

Accepted Solution

NeilJamesCotton earned 167 total points
ID: 34212382
There is a function called DateDif

=DATEDIF(A1,B1,"m")

Where A1 and B1 contain the two dates that you want to compare.

Hope this helps.
0

LVL 8

Assisted Solution

ragnarok89 earned 167 total points
ID: 34212384
If the earlier date is in cell A1 and the later date is in cell A2 :-
=DATEDIF(A1,A2,"m")
This will give the rounded down difference in months.

Al
0

LVL 11

Assisted Solution

Runrigger earned 166 total points
ID: 34212385
datediff("m",datefrom,dateto)
0

LVL 11

Expert Comment

ID: 34212388
Authors request is for VBA version, correct?
0

LVL 1

Expert Comment

ID: 34212400
Sorry, you also need to make sure that the Cells that contain dates are formatted as DataType "DATE"

Highlight the cells in question, right click and choose "Format Cells", from the category list, select DATE and also select the display formatting that you want to format the date in. (The type of display formatting shouldn't effect the formulae) as long as you select an option from the DATE category.

Hope this helps
0

LVL 11

Expert Comment

ID: 34212401
Which version of Excel does the DATEDIF function come in at, my company uses Excel 2007 and it does not exist!
0

LVL 85

Expert Comment

ID: 34212418
DATEDIF is in Excel 2007, just not in the help.
0

LVL 85

Expert Comment

ID: 34212428
Or in the function dropdown, I just noticed.
0

LVL 11

Expert Comment

ID: 34212450
How bizarre not to have help and/or intellisense for it, do you know the reason, I have written my own UDF because I didn't know it existed!

Is there a master list of "hidden" functions out there Rory?
0

LVL 85

Expert Comment

ID: 34212492
That's the only one I know of. I don't think anyone outside MS knows why - see Chip's discussion here: http://www.cpearson.com/Excel/datedif.aspx
0

Author Closing Comment

ID: 34212523
Thanks guys
0

## Featured Post

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,â€¦
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is aâ€¦
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.