How to Use EoMonth in Excel VBA (5 Examples)

Download Practice Workbook


Overview of EoMonth Function in VBA

Function Objective

Returns the serial number for the last day of the month that is the implied number of months before or after start_date.

Syntax

expression.EoMonth (Arg1, Arg2)

The expression represents a WorksheetFunction object.

Arguments Explanations

Argument Required/Optional Data Type Explanations
Arg1 Required Variant Start_date – a date that denotes the starting date. Enter the Dates by using the DATE function, or as a consequence of other formulas or functions. For example, use DATE(2008,5,23) for the 23rd day of May 2008. Dates in Text from will cause an error.
Arg2 Required Variant Months – the number of months before or after start_date. A positive value generates a forthcoming date; a negative value generates a past date.

 

Returns

It returns Double.

Important note:

Directly entering a date in a cell in the format 1/10/2022 will return the serial number 44571. Excel stores a date as a serial number. Change the serial number to date format using the Number Format option in Excel.


5 Examples of EoMonth Function in Excel VBA

To demonstrate our methods, we’ll use the following dataset of some dates in a column:

Suitable Examples of EoMonth Function in Excel VBA


Example 1 – Find the Last Day of the Current Month Using EoMonth

Steps:

  • Press Alt+F11 on your keyboard to open the Visual Basic Application.
  • Select Insert > Module.

  • Enter the following code in the module window:
Sub last_day()

Dim rng As Range
Set rng = Selection
Dim cell As Range

For Each cell In rng
 cell.Offset(0, 1) = Application.WorksheetFunction.EoMonth(cell, 0)
Next cell

End Sub
  • Save the file.
  • Select the range of cells B5:B12.
  • Press Alt+F8 on your keyboard to open the Macro dialog box.

Find the Last Day of Current Month Using EoMonth

  • Select last_day
  • Cick on Run.

Find the Last Day of Current Month Using EoMonth

The dates are displayed as serial numbers.

  • Select all the serial numbers.
  • From the Home tab, click on Number.

  • Select Short Date.

Find the Last Day of Current Month Using EoMonth

The serial numbers are now in date format.

Read more: How to Get the Day of Week Using VBA


Example 2 – Find the Last Day of the Previous Month

To find the last day of the previous month, we just have to enter a negative value in the code used in the previous example.

Steps:

  • Press Alt+F11 on your keyboard to open the Visual Basic Application.
  • Select Insert > Module.

  • Enter the following code in the module window:
Sub previous_month()

Dim rng As Range
Set rng = Selection
Dim cell As Range

For Each cell In rng
 cell.Offset(0, 1) = Application.WorksheetFunction.EoMonth(cell, -1)
Next cell

End Sub
  • Save the file.
  • Select the range of cells B5:B12.
  • Press Alt+F8 to open the Macro dialog box.

  • Select previous_month.
  • Click on Run.

EoMonth to Find the Last Day of the Previous Month in VBA

The dates are shown as serial numbers.

  • Select all the serial numbers.
  • From the Home tab, click on Number.

  • Select Short Date.

EoMonth to Find the Last Day of the Previous Month in VBA

Read more: How to Get the Current Date in VBA


Example 3 – Find the Number of Days in a Month Using EoMonth

Here we’ll combine the EoMonth function with the Day function. The Day function gives us the day of the current date. The EoMonth function will return the last date and the Day function will extract the date as the final day.

Steps:

  • Press Alt+F11 on your keyboard to open the Visual Basic Application.
  • Select Insert > Module.

  • Enter the following code in the module window:
Sub number_of_days()

Dim rng As Range
Set rng = Selection
Dim cell As Range

For Each cell In rng
 cell.Offset(0, 1) = Day(Application.WorksheetFunction.EoMonth(cell, 0)) & " Days"
Next cell

End Sub
  • Save the file.
  • Select the range of cells B5:B12.
  • Press Alt+F8 on your keyboard to open the Macro dialog box.

  • Select number_of_days.
  • Click on Run.

Number of Days in a Month Using EoMonth in VBA

The number of days in each month is returned.

Read more: How to Insert Current Date in Excel


Similar Readings


Example 4 – Find the First Day of a Month Using EoMonth

We will use a negative number in the argument to extract the last date of the previous month, then add 1 to this equation to get the next day, the first day of the next month.

Steps:

  • Press Alt+F11 on your keyboard to open the Visual Basic Application.
  • Select Insert > Module.

  • Enter the following code in the module window:
Sub first_day()

Dim rng As Range
Set rng = Selection
Dim cell As Range

For Each cell In rng
 cell.Offset(0, 1) = Application.WorksheetFunction.EoMonth(cell, -1) + 1
Next cell

End Sub
  • Save the file.
  • Select the range of cells B5:B12.
  • Press Alt+F8 on your keyboard to open the Macro dialog box.

  • Select first_day.
  • Click on Run.

The First Day of a Month Using EoMonth

The dates are shown as serial numbers.

  • Select all the serial numbers.
  • From the Home tab, click on Number.

The First Day of a Month Using EoMonth

  • Select Short Date.

The First Day of a Month Using EoMonth

We have successfully found the first day of a month using the EoMonth function in VBA.

Read more: How to Calculate Due Date with Formula in Excel


Example 5 – Find the End Date of a Future Month

Here, we want to find the end date of a month four months away from our dataset, so we will enter 4 as our EoMonth argument.

Steps

  • Press Alt+F11 on your keyboard to open the Visual Basic Application.
  • Select Insert > Module.

  • Enter the following code in the module window:
Sub future_month()

Dim rng As Range
Set rng = Selection
Dim cell As Range

For Each cell In rng
 cell.Offset(0, 1) = Application.WorksheetFunction.EoMonth(cell, 4)
Next cell

End Sub
  • Save the file.
  • Select the range of cells B5:B12.
  • Press Alt+F8 on your keyboard to open the Macro dialog box.

  • Select future_month.
  • Click on Run.

End Date of a Future Month in VBA

The dates are shown as serial numbers.

  • Select all the serial numbers.
  • From the Home tab, click on Number.

End Date of a Future Month in VBA

  • Select Short Date.

End Date of a Future Month in VBA

We have successfully found the last day of the future month.

Read more: How to Use Excel VBA MONTH Function


Things to Remember

  • If you provide a decimal in the month argument, it will only use the integer portion in start_date.
  • By default, January 1 1900 is serial number 1.
  • If start_date is not a valid date, EoMonth will return the #NUM! error.
  • EoMonth will also return the #NUM! error if start_date plus months produces an invalid date.

Further Reading

Get FREE Advanced Excel Exercises with Solutions!
A.N.M. Mohaimen Shanto
A.N.M. Mohaimen Shanto

A.N.M. Mohaimen Shanto, a B.Sc. in Computer Science and Engineering from Daffodil International University, boasts two years of experience as a Project Manager at Exceldemy. He authored 90+ articles and led teams as a Team Leader, meticulously reviewing over a thousand articles. Currently, he focuses on enhancing article quality. His passion lies in Excel VBA, Data Science, and SEO, where he enjoys simplifying complex ideas to facilitate learning and growth. His journey mirrors Exceldemy's dedication to excellence and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo