VBA Get Previous Date using VBA Functions

VBA Get Previous Date

VBA get Previous Date using Excel VBA Functions. Date plays an important major role in data analysis to summarize data, to create graphs and dashboards based on date. We also format date using format function in multiple ways.

Macro to find Previous Date using VBA

Let us see the procedure how to find previous Date using VBA Date and Format functions.

'Procedure to Get Previous Date
Sub VBA_Previous_Date()
    
    'Variable declaration
    Dim dDate As Date
    
    'Get previous date
    dDate = DateAdd("d", -1, Date)
    
    'Display previous date
    MsgBox "If today's date is " & Date & vbCrLf & _
    "then the previous date is :" & dDate, vbInformation, "Get Previous Date"
    
End Sub

Here is the output screen shot of the above macro procedure.
VBA Get Previous Date

Format Date in different ways:

We have different format methods to format Date. You can find output in an immediate window.

'Format Date
Sub VBA_Format_Date()
    
    'Variable declaration
    Dim dDate As String
    
    dDate = Format(Date, "ddmmyy")
    Debug.Print dDate
    
    dDate = Format(Date, "ddmmyyyy")
    Debug.Print dDate
    
    dDate = Format(Date, "ddmmmyyyy")
    Debug.Print dDate
    
    dDate = Format(Date, "ddmmmyy")
    Debug.Print dDate
    
    dDate = Format(Date, "dd/mmm/yy")
    Debug.Print dDate
    
    dDate = Format(Date, "dd-mmm-yy")
    Debug.Print dDate
    
    dDate = Format(Date, "dddd ddmmmyy")
    Debug.Print dDate
    
    dDate = Format(Date, "dddd dd mmm yy")
    Debug.Print dDate
        
End Sub

Output screenshot: Here is the output screenshot for your reference.

VBA Format Date

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment