Calculate Months Between two Dates in Excel VBA

Calculate Months Between two Dates

Calculate Months Between two Dates in Excel VBA

Calculate Months Between two Dates in Excel VBA using VBA formulas. We use DateDiff VBA Function to determine the number of months between two dates. In order to calculate number of months between two dates we need start date and end date. It returns an integer value as a result. We use DateDif function in Excel to perform same task in Excel.

Macro to Calculate Number of Months Between two Dates in Excel VBA

Let us see the macro to calculate Months Difference Between two Dates in Excel VBA.

'Calculate Months Between two Dates in Excel VBA
Sub VBA_Calculate_Months_Between_Two_Dates()
    
    'Variable declaration
    Dim sDate As Date, eDate As Date
    Dim iValue As Integer
    
    'Start Date
    sDate = "10-Jan-2020"
    
    'End Date
    eDate = "15-Sep-2020"
    
    iValue = DateDiff("m", sDate, eDate)
    
    'Display output on the screen
    MsgBox "The number of months between two dates : " & iValue, vbInformation, "Months Between Two Dates"
    
End Sub

Here is the output screen shot of above macro.
Calculate Months Between two Dates

Instructions to use Macro

Here are the instructions to use above macro in Visual basic editor.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can see output on the screen
  • Find above specified output screenshot.

Related VBA Functions used in this article

We have used Date and DateDiff VBA functions. Click on below specified links to learn more about VBA functions.

VBA DateDiff Function VBA Date Function VBA Month Function






Leave a Comment

Your email address will not be published. Required fields are marked *