Dates in VBA: calculate, compare, format

Payment terms, overdue invoices, delivery dates that must not land on a weekend: office work is full of date arithmetic. VBA has its own date functions, and they get month lengths and leap years right without you thinking about it.

In short: Date is today, DateSerial(2026, 3, 16) builds a date from year, month and day, DateAdd("d", 14, d) adds 14 days, DateDiff("d", fromDate, toDate) counts the days in between, and Weekday(d, vbMonday) returns 1 for Monday up to 7 for Sunday. Format(d, "dd mmm yyyy") turns a date into text.

What you need to know: For Next and Select Case.

Try it right now

Task Every invoice is due 14 days after the invoice date. Write a macro DueDates that uses DateAdd to work out the due date for every row from 2 and writes it to column C.

ABCD
1InvoiceDateDue date
2INV-204120 Feb 2026
3INV-204224 Feb 2026
4INV-20433 Mar 2026
5INV-20449 Mar 2026
6INV-204512 Mar 2026

How Excel and VBA store a date

To Excel, a date is a serial number: each day counts one up, and the time of day is the fraction after the decimal point. Only the cell's number format turns it into 16 Mar 2026. VBA has the Date data type for this. Because there is a number underneath, you can compare dates with < and > and move on a day with + 1.

FunctionReturnsExample (today = 16 Mar 2026)
Datetoday's date16 Mar 2026
Nowtoday with the time16 Mar 2026 09:30
DateSerial(y, m, d)a date from year, month, dayDateSerial(2026, 3, 1) = 1 Mar 2026
Year, Month, Dayparts of a date as numbersMonth(Date) = 3
DateAdd(interval, n, d)date plus n days ("d"), months ("m") or years ("yyyy")DateAdd("m", 1, DateSerial(2026, 1, 31)) = 28 Feb 2026
DateDiff(interval, from, to)the gap between two datesDateDiff("d", DateSerial(2026, 3, 1), Date) = 15
Weekday(d, vbMonday)day of the week, Monday = 1 to Sunday = 7Weekday(Date, vbMonday) = 1
Format(d, code)a date as textFormat(Date, "dddd d mmmm yyyy") = Monday 16 March 2026

DateSerial rolls over month boundaries: DateSerial(2026, 4, 0) is the day before 1 April, so 31 Mar 2026. That gives you the last day of a month without knowing how long the month is. DateAdd with "m" stays inside the target month: one month after 31 January is 28 February.

For fixed dates in code, use DateSerial. Date literals such as #3/16/2026# exist too, but they are always month/day/year, whatever the regional settings. DateSerial spells out which number is the month, so nobody has to guess.

Counting days up to today

DateDiff subtracts the first date from the second, so with the earlier date first the result is positive. In the exercises, "today" is always Monday 16 Mar 2026, so the results are the same whenever you try them. In real Excel, Date returns your computer's date.

Task Today is Monday 16 Mar 2026 (Date in VBA). Write a macro DaysOverdue that uses DateDiff to count the days from the due date to today for every invoice from row 2. If the invoice is overdue (more than 0 days), the number goes in column C. If it is due today or later, C stays empty.

ABCD
1InvoiceDue dateDays overdue
2INV-203126 Feb 2026
3INV-203419 Mar 2026
4INV-20369 Mar 2026
5INV-203816 Mar 2026
6INV-20409 Feb 2026

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Days of the week and weekends

Without a second argument, Weekday counts from Sunday: Sunday is 1, Saturday 7. With vbMonday the week starts on Monday, and 6 and 7 are the weekend. Always pass the second argument; then nobody has to guess which count you mean.

Task Delivery is 3 days after the order, but never at the weekend. Write a macro DeliveryDate that takes the order date plus 3 days for every row from 2 and, if that falls on a Saturday or Sunday, moves it to the following Monday. The result goes in column C. Use Weekday with vbMonday.

ABCD
1OrderOrderedDelivery
2PO-510116 Mar 2026
3PO-510218 Mar 2026
4PO-510319 Mar 2026
5PO-510420 Mar 2026
6PO-510521 Mar 2026

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

Common mistakes

Weekday without vbMonday

If you expect Saturday to be 6 but write Weekday(delivery) without the second argument, you are really testing for Friday. A Saturday is then 7 and only gets moved by one day, onto the Sunday. The exercise reports: "C3 contains 22 Mar 2026, expected: 23 Mar 2026."

DateDiff the wrong way round

DateDiff("d", Date, dueDate) counts from today to the due date. For overdue invoices the result is negative, and the If writes nothing. Put the earlier date first.

Format written into the cell

Cells(r, 3).Value = Format(d, "dd mmm yyyy") writes text, not a date. Formulas cannot calculate with it, and sorting goes by letters. Write the date itself into the cell and set its look with NumberFormat from Format cells.

A made-up interval

Intervals are fixed codes: "d" for day, "m" for month, "yyyy" for year, "ww" for week. Anything else, such as DateAdd("t", 14, d), gives "Run-time error '5': Invalid procedure call or argument".

In real Excel

Dates from cells arrive in VBA as Date values when the cell holds a real date. If it holds text that only looks like a date (common after an import), VBA has to convert it first, and how it reads it depends on the computer's regional settings. Check such cells with IsDate, or turn them into real dates first. A quick way to tell in Excel: with no alignment set, real dates sit on the right of the cell, text on the left.

FAQ

How do I get the week number?

It depends on the convention. For ISO weeks (Monday start, used in the UK and much of Europe), use WorksheetFunction.IsoWeekNum(d). For the US count, where weeks start on Sunday and the week containing 1 January is week 1, use WorksheetFunction.WeekNum(d).

How do I work with business days?

WorksheetFunction.WorkDay(start, days) and WorksheetFunction.NetworkDays(fromDate, toDate), just like WORKDAY and NETWORKDAYS. Pass public holidays as a range in the third argument.

What is the difference between Date and Now?

Date is just the day; Now adds the time. Compare due dates with Date, otherwise an invoice due today already looks "older" than today because of the time.

How do I put a date in a MsgBox?

With Format: MsgBox "As of " & Format(Date, "d mmm yyyy"). Format is exactly right for text, just not for cells you want to calculate with.

Learn this next: user-defined functions

Write your own functions with Function, use them in a cell just like SUM, and call them from your macros.

Write your own function

Write to us

Choose language

Select your preferred language