> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Looping by Month: Loop Until with DateAdd
- URL: https://nolongerset.com/looping-by-month-loop-until-with-dateadd/
- Published: 2021-02-26T03:31:09.000Z
- Updated: 2026-05-08T13:06:10.000Z
- Description: Reader Francesco Foti writes in with his own solution to the Looping by Month challenge.
- Author: Mike Wolfe
- Tags: Code Library, #Date Functions, #Import 2026-05-20 02:59

This is the fourth in a [series of articles](https://nolongerset.com/sticky-wicket-looping-by-month/) about looping code one month at a time.

Writing a loop that iterates one month at a time is a subtly tricky problem. The difficulty isn't so much in writing the code itself. The challenge is to write the code in such a way that your intent is clear to the reader.

### Reader Submission

One of the things I find endlessly fascinating about programming is the myriad solutions to solving the same problem. The collective knowledge of the internet makes finding these sorts of alternative solutions easier than ever before.

This fourth method of looping by month was submitted by friend of the blog, Francesco Foti ([@francescofoti](https://twitter.com/francescofoti)). He's got a great Access blog of his own over at <http://francescofoti.com/>. Be sure to check it out.

> Tried your code (btw: Private Const MonthsInYear As Integer = 12). Nice hack on DateSerial to get the last day in month ;-). Just for fun, here's how I would have done it (quick & dirty, native); harder to read ? (and, apparently, no "mod" on month necessary with DateSerial) [pic.twitter.com/otfVQK1Ph1](https://t.co/otfVQK1Ph1)
> 
> — francesco (@francescofoti) [February 24, 2021](https://twitter.com/francescofoti/status/1364498056553594881?ref%5Fsrc=twsrc%5Etfw)

### Looping by month using DateAdd() and Loop Until

Here's Francesco's code in copy/paste-able format. I modified it slightly to accept a StartDate and EndDate parameter:

```vba
Sub LoopUntilMonths(StartDate As Date, EndDate As Date)
    Dim vThisDate As Variant
    Dim iYear     As Integer
    Dim iMonth    As Integer
    Dim vMonthEnd As Variant
    
    vThisDate = StartDate
    Do
      iYear = Year(vThisDate)
      iMonth = Month(vThisDate)
      vMonthEnd = DateSerial(iYear, iMonth + 1, 0)
      Debug.Print vThisDate, iMonth, iYear, vMonthEnd
      vThisDate = DateAdd("m", 1, vThisDate)
    Loop Until vThisDate > EndDate

End Sub
```

I was curious to know how the [DateAdd](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dateadd-function)() function would handle incrementing by month from a date on the 31st day of the month when the following month had only 30 days. It turns out that it if the following month does not have enough days, then the function returns whatever the last day of the month is.

An example makes this clearer:

```vba
LoopUntilMonths #10/31/2020#, #3/31/2021#
10/31/2020     10            2020         10/31/2020 
11/30/2020     11            2020         11/30/2020 
12/30/2020     12            2020         12/31/2020 
1/30/2021      1             2021         1/31/2021 
2/28/2021      2             2021         2/28/2021 
3/28/2021      3             2021         3/31/2021 
```

### Readability

Whether you find this approach more readable than some of the others may be as much about personal preference and coding style as anything else. One thing I do really like about this approach is the way that the StartDate and EndDate lines set off the loop.

I think visual cues are incredibly important when it comes to code readability. When I read the code above, my mind intuitively groups all the code between the line with StartDate and the line with EndDate and registers that as, "This is all the stuff that happens as we move from the start date to the end date."

Thanks again for engaging, Francesco!

*Image by [It is not permitted to sell my photos with StockAgencies](https://pixabay.com/users/lmoonlight-236255/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3260697) from [Pixabay](https://pixabay.com/?utm%5Fsource=link-attribution&utm%5Fmedium=referral&utm%5Fcampaign=image&utm%5Fcontent=3260697) (editor's note: That's a weird name for a child, but it's [probably real](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/))*