How can I subtract days from a date using VBA?

Subtracting days from a date using VBA can be done by subtracting the desired number of days from the date and then using the DateAdd function with the parameter interval set to “d” for days to get the resulting date. For example, to subtract 10 days from a date, you would enter DateAdd(“d”, -10, ) to get the desired result.


You can use the DateAdd function in VBA with a negative number to subtract a specific number of days from a date.

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

Sub SubtractDays()

    Dim i As Integer
    
    For i = 2 To 10
        Range("B" & i) = DateAdd("d", -4, Range("A" & i))
    Next i
    
End Sub

This particular macro will subtract four days from each date in the range A2:A10 and display the new dates in the range B2:B10.

Note that the “d” argument in the DateAdd function specifies that we would like to subtract days to the dates as opposed to another unit of time.

Refer to the for a complete list of units you can use in the DateAdd function.

The following example shows how to use this syntax in practice.

Example: Subtract Days from Date in VBA

Suppose we have the following list of dates in Excel:

Suppose we would like to subtract four days from each date and display the new dates in column B.

We can create the following macro to do so:

Sub SubtractDays()

    Dim i As Integer
    
    For i = 2 To 10
        Range("B" & i) = DateAdd("d", -4, Range("A" & i))
    Next i
    
End Sub

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

VBA subtract days from date

Notice that column B contains each of the dates in column A with four days subtracted from them.

Feel free to change the numeric value in the DateAdd function to subtract a different number of days from each date.

x