0

Working on populating a row in excel with dates between a start date and current date. The population is weekly and below is the function I have made. It works fine up until the point where it doesn't stop but continues to go infinitely until there is an overflow error hence my assumption is that CurrentDate is not working properly.

The 2 dates used are StartDate = 04/1/2016 and CurrentDate = 12/07/2017.

Any help or suggestions would be greatly appreciated.

Public Function PopulateStartOfWeekDates()

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets("CRC")

    Dim StartDate As Date
    Dim CurrentDate As Date
    StartDate = FirstMondayOfYear()
    CurrentDate = Date

    Dim WeekOffset As Integer
    Dim i As Integer
    i = 12
    WeekOffset = 0

    Debug.Print StartDate
    Debug.Print CurrentDate

    Do While StartDate < CurrentDate        
        wsCRC.Cells(5, i) = StartDate + WeekOffset
        wsCRC.Cells(5, i).EntireColumn.AutoFit
        i = i + 1
        WeekOffset = WeekOffset + 7                    
    Loop

End Function
3
  • you need to share your FirstMondayOfYear function, what value are you getting for StartDate when running in debug mode ? Commented Jul 12, 2017 at 8:01
  • 4
    StartDate remains the same in the loop, unless im mistaken? Commented Jul 12, 2017 at 8:03
  • Also, where do you increment the value of StartDate inside the loop Do While StartDate < CurrentDate ? don't you mean to add at the end StartDate = StartDate +7 ? Commented Jul 12, 2017 at 8:03

2 Answers 2

2

If you decide you need to maintain the value of StartDate (e.g. to use later in the code), you could replace your loop with:

i = 0
Do While StartDate + i * 7 < CurrentDate
    wsCRC.Cells(5, i + 12) = StartDate + i * 7
    wsCRC.Cells(5, i + 12).EntireColumn.AutoFit
    i = i + 1
Loop
Sign up to request clarification or add additional context in comments.

1 Comment

That' a good idea ill keep this in mind as it definitely looks useful, but I use another function to get the start date so I don't particularly need to implement this right now
0

After looking at this myself I realized I wasn't increasing the startdate hence the loop was infinite. Thanks to @Nathan_Sav for pointing this out in the comments too.

1 Comment

I always add a break point at things like wend and loop when working, just in case :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.