Excel

Return number of days in a given month; Function

Ease of Use

Easy

Version tested with

2002, 2003 

Submitted by:

Zack Barresse

Description:

This function will return the number of days for a given month. The month needs to be given as a numeric (1-12). 

Discussion:

The need may arise to return the number of days in a month for calculations. This can be done with native functions, but this is a much more condensed version and can save formula real-estate. 

Code:

instructions for use

			

Option Explicit Public Function MonthDays(myMonth As Long) As Long MonthDays = Day(DateSerial(Year(Date), myMonth + 1, 1) - 1) End Function

How to use:

  1. Copy above code.
  2. In Excel press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save workbook before any other changes.
 

Test the code:

  1. Install the code.
  2. Save workbook.
  3. In a blank cell, enter "=MonthDays(1)"
  4. Replace the number 1 with the desired month number.
 

Sample File:

MonthDaysEx.zip 7.92KB 

Approved by mdmackillop


This entry has been viewed 207 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express