How to Use DateSerial Function in VBA (With Example)


You can use the DateSerial function in VBA to return a date for a specific day, month and year.

Here is one common way to use this function in practice:

Sub UseDateSerial()
    
Dim i As Integer

For i = 2 To 13
    Range("D" & i) = DateSerial(Range("C" & i), Range("B" & i), Range("A" & i))
Next i
    
End Sub

This particular macro will return date values in the range D2:D13 using the year values in column C, the month values in column B, and the day values in column A.

The following example shows how to use the DateSerial function in practice.

Example: How to Use DateSerial Function in VBA

Suppose we have the following columns in Excel that show the day, month and year for various dates:

Suppose we would like to display date values in column D, using the day, month and year values in each row.

We can create the following macro to do so:

Sub UseDateSerial()
    
Dim i As Integer

For i = 2 To 13
    Range("D" & i) = DateSerial(Range("C" & i), Range("B" & i), Range("A" & i))
Next i
    
End Sub

When we run this macro, we receive the following output:

Column D displays the date value that results from using the day, month and year values from columns A, B and C, respectively.

Note: You can find the complete documentation for the VBA DateSerial function here.

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

How to Compare Dates in VBA
How to Convert Date to Week Number in VBA
How to Calculate Days Between Two Dates in VBA

Featured Posts

Leave a Reply

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