Looping through multiple rows of dates whilst interating dates inbetween

MaxD

New Member
Joined
Aug 18, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi MrExcel troops,

My time has finally come to ask a question rather than rely on all the answers as I'm having a brain freeze moment!

I've got a dataset showing the input table on the left & the output table on the right. The first date range interates as I'd expect, however I can't get the next rows to iterate their date ranges or indeed the other cells to be populated. Step 1 is to get the dates to iterate.

VBA Code:
Sub GenerateDates()
    Application.ScreenUpdating = False
    
    Dim i, j As Integer
    Dim finalRow, finalRow2, startRow As Long
    
    finalRow = Range("A" & Rows.Count).End(xlUp).Row
    finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
    
    For i = 2 To 4 'finalRow
        finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
        startRow = finalRow2 + 1
    
        Range("M" & startRow) = Range("A" & i)
        
        j = i
        
        Days = Int((Range("B" & i) - Range("A" & i)))
           
        Do While j < 2 + Days
            j = j + 1
            
            Range("M" & j) = Range("M" & j - 1) + 1
        Loop
        
        Range("N" & startRow) = Range("C" & i)
        Range("O" & startRow) = ""
        Range("P" & startRow) = Range("F" & i)
        Range("Q" & startRow) = Range("G" & i)
        
    Next i
    
End Sub

Creating the mini-sheet keeps crashing my Excel, so here's the input and output table:

220818-120558.jpg


What I'm trying to achieve:

220818-120753.jpg
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Fixed the issue with the mini-sheet creation:


Book4
ABCDEFGLMNOPQ
1Start dateEnd dateTypeStart dateEnd date portionNameCountryDateTypeDayNameCountry
212/09/202217/09/2022Holiday5Name 1United States
315/08/202219/08/2022Time off in lieu5Name 2United Kingdom
406/10/202211/10/2022Holiday4Name 2United Kingdom
515/08/202219/08/2022Holiday5Name 3Hungary
602/09/202202/09/2022Holiday1Name 4United States
725/11/202225/11/2022Holiday1Name 5United Kingdom
826/12/202230/12/2022Holiday4Name 6United Kingdom
915/08/202219/08/2022Holiday5Name 6United Kingdom
Sheet1
 
Upvote 0
Step 1: resolved

VBA Code:
For i = 2 To finalRow

    startDate = Range("A" & i).Value
    endDate = Range("B" & i).Value
    startDate = startDate - 1

    Do While startDate < endDate
        finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
        finalRow2 = finalRow2 + 1
        startDate = DateAdd("d", 1, startDate)
        Range("M" & finalRow2).Value = startDate
    Loop

Next i
 
Upvote 0
Full code fix below:

VBA Code:
Sub GenerateDates()

Application.ScreenUpdating = False

Dim i, j, numDays As Integer
Dim finalRow, finalRow2 As Long
Dim startDate, endDate As Date
Dim strType, strName, strCountry As String

finalRow = Range("A" & Rows.Count).End(xlUp).Row
finalRow2 = Range("M" & Rows.Count).End(xlUp).Row

If finalRow2 > 1 Then
    Range("M2:Q" & finalRow2).Select
    Selection.ClearContents
End If

Range("A1").Select

For i = 2 To finalRow
    startDate = Range("A" & i).Value
    endDate = Range("B" & i).Value
    startDate = startDate - 1
    strType = Range("C" & i).Value
    numDays = Range("D" & i).Value
    strName = Range("F" & i).Value
    strCountry = Range("G" & i).Value

    Do While startDate < endDate
        finalRow2 = Range("M" & Rows.Count).End(xlUp).Row
        finalRow2 = finalRow2 + 1
        startDate = DateAdd("d", 1, startDate)
        Range("M" & finalRow2).Value = startDate
        Range("N" & finalRow2).Value = strType
        'Function to split decimal numDays
        Range("P" & finalRow2).Value = strName
        Range("Q" & finalRow2).Value = strCountry
    Loop
Next i

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,836
Messages
6,127,173
Members
449,368
Latest member
JayHo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top