VBA Previous Month Name

VBA Previous Month Name

VBA Previous Month Name from Today’s Date

Get Previous Month Name From Today’s Date using Excel VBA Functions. Name of the Previous month plays an important major role of a date. We can get Previous month name using month function and can summarize data with it. Once we get month name we can Format name of the month by using format function as per our requirement.

VBA Find Name of the Previous Month:

Let us see the procedure how to get name of the month from today’s date using MonthName function.

'Procedure to find Name of the Previous Month From Date
Sub VBA_Get_Previous_Month_Name()
    
    'Variable declaration
    Dim sMonth_Name As String
    
    'Retrieve month Name from date
    sMonth_Name = MonthName(Month(DateAdd("m", -1, Date)))

    'Display month name
    MsgBox "If Previous month is '" & MonthName(Month(Date)) & "' then" & vbCrLf & _
    "the Previous month name is : '" & sMonth_Name & "'", vbInformation, "Previous Month Name"
    
End Sub

Explanation: In the above procedure we have used DateAdd, Month, Date and MonthName VBA functions. Here Month function is used to display month number. MonthName function helps to generate name of the month from specified month number. Here is the output screenshot for your reference.

VBA Previous Month Name

Format Name of the Month:

We have different format methods to format name of the month. You can find output in an immediate window.

'Format Name of the Month
Sub VBA_Format_Month_Name()
    
    'Variable declaration
    Dim sMonth_Name As String
    
    sMonth_Name = Format("01/01/2018", "m")
    Debug.Print sMonth_Name
    
    sMonth_Name = Format("01/01/2018", "mm")
    Debug.Print sMonth_Name
    
    sMonth_Name = Format("01/01/2018", "mmm")
    Debug.Print sMonth_Name
    
    sMonth_Name = Format("01/01/2018", "mmmm")
    Debug.Print sMonth_Name
        
End Sub

Output Screenshot:

Here is the output screenshot of the above macro procedure. we have shown four different format methods.
Format Month Name

Instructions to use Macro:

  • Open Visual Basic Editor by clicking Alt +F11
  • Goto code window by clicking F7
  • Copy any of the above specified procedure or macro
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • You can see ouput on the screen
  • Find specified output screenshot above.

Related VBA Functions used in this article:

We have used Date,Month,MonthName and Format VBA functions. Click on below specified links to learn more about VBA functions.

VBA DateAdd Function VBA Date Function VBA Month Function VBA MonthName Function

VBA Format Function






Leave a Comment

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