VBA - Help - Idea Needed to Insert Rows if the next date is not consecutive.

Trying2learnVBA

Board Regular
Joined
Aug 21, 2019
Messages
67
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello,
I am not quite sure how to properly phrase my question.
I am trying to code a macro to insert rows if the date is not the next day.
But I am trying to keep repeated dates.
The highlighted dates came from my data set. The none highlighted dates is where I need rows to be inserted as needed.
Book3
BJKL
22Days with ActivityDateResult Needed
23108/01/2208/01/22
24108/01/2208/01/22
25108/01/2208/01/22
26108/01/2208/01/22
27108/01/2208/01/22
2858/5/20228/2/2022VBA to insert rows if the date is not consecutive
2958/5/20228/3/2022Or VBA to insert rows if the Days are not consecutive
3088/8/20228/4/2022
3188/8/20228/5/2022
3288/8/20228/6/2022
3388/8/20228/7/2022
34128/12/20228/8/2022
35128/12/20228/8/2022
36128/12/20228/8/2022
37158/15/20228/8/2022
38158/15/20228/9/2022
39178/17/20228/10/2022
40178/17/20228/11/2022
41198/19/20228/12/2022
42228/22/20228/12/2022
43228/22/20228/12/2022
44248/24/20228/13/2022
45248/24/20228/14/2022
46268/26/20228/15/2022
47268/26/20228/15/2022
48308/30/20228/16/2022
49308/30/20228/17/2022
50318/31/20228/17/2022
51318/31/20228/18/2022
528/19/2022
538/20/2022
548/21/2022
558/22/2022
568/22/2022
578/23/2022
588/24/2022
598/24/2022
608/25/2022
618/26/2022
628/26/2022
638/27/2022
648/28/2022
658/29/2022
668/30/2022
678/30/2022
688/31/2022
698/31/2022
Advances.


Here - i am trying to play using the mid formula to extract the date. But I simply want the proper number of rows to be inserted when the numbers or dates are not consecutive. But I do want to keep repeated dates.

Please point me in the right direction.
I can't quite come up with the right logic for an If else statement.

Thank you in advance gurus!!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Posting your code (please use code tags - vba button on posting toolbar) would give anyone something to start with. I presume you simply forgot one 8/5 value in column 3?

Or loop over the range and if date value isn't greater by one, calculate the difference. Either loop that many times then insert and shift cells down, or pass the value to another procedure that does the same.
 
Upvote 0
So you want to create a new list in column K, with the missing days added? Maybe this macro:
VBA Code:
Sub AddDaysJtoK()
Dim I As Long, dGap As Long, J As Long
Dim Extra As Long
'
Cells(2, "K") = Cells(2, "J")
For I = 3 To Cells(Rows.Count, "J").End(xlUp).Row
    dGap = Cells(I, "J") - Cells(I - 1, "J")
    If dGap > 1 Then
'        Cells(I, "J").Resize(dGap - 1, 1).EntireRow.Insert Shift:=xlDown
        For J = 1 To dGap - 1
            Cells(I + Extra, "K") = Cells(I - 1, "J").Value + J
            Extra = Extra + 1
        Next J
        Cells(I + Extra, "K") = Cells(I, "J")
    Else
        Cells(I + Extra, "K") = Cells(I, "J")
    End If
Next I
MsgBox ("Completed")
End Sub
Try...
 
Upvote 0
So you want to create a new list in column K, with the missing days added? Maybe this macro:
VBA Code:
Sub AddDaysJtoK()
Dim I As Long, dGap As Long, J As Long
Dim Extra As Long
'
Cells(2, "K") = Cells(2, "J")
For I = 3 To Cells(Rows.Count, "J").End(xlUp).Row
    dGap = Cells(I, "J") - Cells(I - 1, "J")
    If dGap > 1 Then
'        Cells(I, "J").Resize(dGap - 1, 1).EntireRow.Insert Shift:=xlDown
        For J = 1 To dGap - 1
            Cells(I + Extra, "K") = Cells(I - 1, "J").Value + J
            Extra = Extra + 1
        Next J
        Cells(I + Extra, "K") = Cells(I, "J")
    Else
        Cells(I + Extra, "K") = Cells(I, "J")
    End If
Next I
MsgBox ("Completed")
End Sub
Try...
Ok This is exactly what I am trying to do..
TEmp Adv & Mats.xlsm
ABJKLMNOADAEAF
1AdvancesDate - Month-Day-YearMid Formula to get the day
2Advance TypeAdvance Number.Advance Amount.Funding Date.Maturity Date.Initial Interest Rate.DayDiff
3FD-FHLB_ADV_LT_3M361450144,830.0008/01/20229/26/20222.42%1-11
4FD-FHLB_ADV_LT_3M361450244,839.0008/05/202210/5/20222.45%53Insert 3 rows
5FD-FHLB_ADV_LT_3M361450344,826.0008/08/20229/22/20222.41%82
6FD-FHLB_ADV_LT_3M361450444,827.0008/08/20229/23/20222.41%8-1
7FD-FHLB_ADV_LT_3M361450544,841.0008/08/202210/7/20222.50%8-15
8FD-FHLB_ADV_LT_3M361450644,847.0008/15/202210/13/20222.60%156Insert 2 rows
9FD-FHLB_ADV_LT_3M361450744,851.0008/17/202210/17/20222.57%171
10FD-FHLB_ADV_LT_3M361450844,853.0008/22/202210/19/20222.68%2248
11FD-FHLB_ADV_LT_3M361450944,855.0008/26/202210/21/20222.75%2638
12FD-FHLB_ADV_LT_3M361451044,832.0008/30/20229/28/20222.51%3038
13FD-FHLB_ADV_LT_3M361451144,859.0008/30/202210/25/20222.80%30-1
14FD-FHLB_ADV_LT_3M361451244,845.0008/31/202210/11/20222.68%310Insert 6 rows
15FD-FHLB_ADV_LT_3M361451344,861.0008/31/202210/27/20222.88%31-1
16Total13582,966.00
17
18I Need a loop to insert rows if (O3 & LR) is >0
19The number of rows to be inserted is the diff number in colum (O)15
20
21For instance, I need 4 rows to be inserted below row 3. Then 3 rows to be inserted then skip the zeroes and insert 7 rows insert 2 rows etc.17
22End result should be all the days in a given month. 1-31. I'll have
23All the way to 8/31/2022
Advances.
Cell Formulas
RangeFormula
K3:K15K3=TEXT([@[Funding Date]],"Mm/DD/YYYY")
L3:L15L3=[@[Maturity Date]]
M3:M15M3=[@[Initial Interest Rate]]/100
N3:N15N3=MID([@[.Funding Date]],4,2)*1
O3O3=([@[.Day]]-1)-1
O4:O15O4=([@[.Day]]-N3)-1
B16B16=SUBTOTAL(103,[Advance Number])
J16J16=SUBTOTAL(109,[.Advance Amount])


This is the code I am trying to make work..
VBA Code:
Sub AddDaysJtoK()
'Dim I As Long, dGap As Long, J As Long
'Dim Extra As Long
''
'Cells(2, "B") = Cells(2, "C")
'For I = 3 To Cells(Rows.Count, "B").End(xlUp).Row
'    dGap = Cells(I, "C") - Cells(I - 1, "B")
'    If dGap > 1 Then
''        Cells(I, "J").Resize(dGap - 1, 1).EntireRow.Insert Shift:=xlDown
'        For C = 1 To dGap - 1
'            Cells(I + Extra, "C") = Cells(I - 1, "B").Value + B
'            Extra = Extra + 1
'        Next C
'        Cells(I + Extra, "C") = Cells(I, "B")
'    Else
'        Cells(I + Extra, "C") = Cells(I, "B")
'    End If
'Next I
'MsgBox ("Completed")



Dim WB As WB, Adv As WS, Mats As WS
Set WB = ActiveWorkbook
Set Adv = WB.Sheets("Advances")
Set Mats = WB.shetets("Maturities")

With Adv
LastRow1 = Adv.Cells(Adv.Rows.Count, 1).End(xlUp).Row
If .Range("O3" & LastRow) > 0 Then




End Sub
 
Upvote 0
And did you try my code?
Yes - it gives an error after the first date. Your code is what I have commented out to fit my spreadsheet. I am not sure if I needed column "O" but that's just how I am trying to make it work

would you please add comments as to what each line is doing. I am not good at all with For code.

What does Cells(2, "B") = etc mean, what is the 2 referencing to?
to change it to column O would it be "Cells(15, "O")... ?
For I = 3 - what is this doing?
 
Last edited:
Upvote 0
Posting your code (please use code tags - vba button on posting toolbar) would give anyone something to start with. I presume you simply forgot one 8/5 value in column 3?

Or loop over the range and if date value isn't greater by one, calculate the difference. Either loop that many times then insert and shift cells down, or pass the value to another procedure that does the same.
You're correct - I simply forgot one 8/5
 
Upvote 0
And did you try my code?
OK,

Based on this exact sheet here:
TEmp Adv & Mats.xlsm
ABCDEFGHI
2Advance TypeAdvance NumberAdvance AmountTrade DateFunding DateMaturity DateInitial Interest RatePayment FrequencyFirst Payment Due Date
3FD-FHLB_ADV_LT_3M3614501150,000,000.008/1/202208/02/229/26/20222.42609/26/2022 0:00
4FD-FHLB_ADV_LT_3M3614502100,000,000.008/5/202208/05/2210/5/20222.456010/5/2022 0:00
5FD-FHLB_ADV_LT_3M3614503150,000,000.008/8/202208/08/229/22/20222.41609/22/2022 0:00
6FD-FHLB_ADV_LT_3M3614504150,000,000.008/8/202208/08/229/23/20222.41609/23/2022 0:00
7FD-FHLB_ADV_LT_3M3614505150,000,000.008/8/202208/08/2210/7/20222.56010/7/2022 0:00
8FD-FHLB_ADV_LT_3M3614506100,000,000.008/15/202208/15/2210/13/20222.66010/13/2022 0:00
9FD-FHLB_ADV_LT_3M3614507100,000,000.008/17/202208/17/2210/17/20222.576010/17/2022 0:00
10FD-FHLB_ADV_LT_3M3614508150,000,000.008/22/202208/22/2210/19/20222.686010/19/2022 0:00
11FD-FHLB_ADV_LT_3M3614509100,000,000.008/26/202208/26/2210/21/20222.756010/21/2022 0:00
12FD-FHLB_ADV_LT_3M3614510150,000,000.008/30/202208/30/229/28/20222.51309/28/2022 0:00
13FD-FHLB_ADV_LT_3M3614511150,000,000.008/30/202208/30/2210/25/20222.86010/25/2022 0:00
14FD-FHLB_ADV_LT_3M3614512100,000,000.008/31/202208/31/2210/11/20222.686010/11/2022 0:00
15FD-FHLB_ADV_LT_3M3614513100,000,000.008/31/202208/31/2210/27/20222.886010/27/2022 0:00
Advances

I've changed your code as best as I understand it to fit my needs.
The debugger sayd dGap=0 - I changed the date to 08/02/2022 from 08/01/2022 to see if this would make dGap not be zero
VBA Code:
Sub AddDaysJtoK()
Dim I As Long, dGap As Long, J As Long
Dim Extra As Long
'
Cells(2, "E") = Cells(2, "J")
For I = 3 To Cells(Rows.Count, "J").End(xlUp).Row
    dGap = Cells(I, "E") - Cells(I - 1, "J")
    If dGap > 1 Then
'        Cells(I, "J").Resize(dGap - 1, 1).EntireRow.Insert Shift:=xlDown - IS THIS SUPPOSED TO BE COMMENTED OUT OR WAS THIS A TYPO?
        For J = 1 To dGap - 1
            Cells(I + Extra, "E") = Cells(I - 1, "J").Value + J
            Extra = Extra + 1
        Next J
        Cells(I + Extra, "E") = Cells(I, "J")
    Else
        Cells(I + Extra, "E") = Cells(I, "J")
    End If
Next I
MsgBox ("Completed")

End Sub
 
Upvote 0
Was beginning to feel ignored ;) . I got this
1​
8/01/22​
1​
8/01/22​
1​
8/01/22​
1​
8/01/22​
1​
8/01/22​
8/02/22​
8/03/22​
8/04/22​
5​
8/05/22​
5​
8/05/22​
8/06/22​
8/07/22​
8​
8/08/22​
8​
8/08/22​
8​
8/08/22​
8​
8/08/22​
8/09/22​
8/10/22​
8/11/22​
12​
8/12/22​
12​
8/12/22​
12​
8/12/22​
8/13/22​
8/14/22​
15​
8/15/22​
15​
8/15/22​
8/16/22​
17​
8/17/22​
17​
8/17/22​
8/18/22​
19​
8/19/22​
8/20/22​
8/21/22​
22​
8/22/22​
22​
8/22/22​
8/23/22​
24​
8/24/22​
24​
8/24/22​
8/25/22​
26​
8/26/22​
26​
8/26/22​
8/27/22​
8/28/22​
8/29/22​
30​
8/30/22​
30​
8/30/22​
31​
8/31/22​
31​
8/31/22​
with this
VBA Code:
Sub insertDates()
Dim rng As Range
Dim i As Integer, Lrow As Integer

Lrow = Cells(Rows.count, "I").End(xlUp).Row
For Each rng In Range("I2:I" & Lrow)
  If rng.Offset(1, 0) - rng > 1 Then
    For i = 1 To (rng.Offset(1, 0) - rng) - 1
       rng.Offset(i, 0).EntireRow.Insert Shift:=xlDown
       rng.Offset(i, 0) = rng + i
     Next
  End If
Next

End Sub
 
Upvote 0
Solution
Was beginning to feel ignored ;) . I got this
1​
8/01/22​
1​
8/01/22​
1​
8/01/22​
1​
8/01/22​
1​
8/01/22​
8/02/22​
8/03/22​
8/04/22​
5​
8/05/22​
5​
8/05/22​
8/06/22​
8/07/22​
8​
8/08/22​
8​
8/08/22​
8​
8/08/22​
8​
8/08/22​
8/09/22​
8/10/22​
8/11/22​
12​
8/12/22​
12​
8/12/22​
12​
8/12/22​
8/13/22​
8/14/22​
15​
8/15/22​
15​
8/15/22​
8/16/22​
17​
8/17/22​
17​
8/17/22​
8/18/22​
19​
8/19/22​
8/20/22​
8/21/22​
22​
8/22/22​
22​
8/22/22​
8/23/22​
24​
8/24/22​
24​
8/24/22​
8/25/22​
26​
8/26/22​
26​
8/26/22​
8/27/22​
8/28/22​
8/29/22​
30​
8/30/22​
30​
8/30/22​
31​
8/31/22​
31​
8/31/22​
with this
VBA Code:
Sub insertDates()
Dim rng As Range
Dim i As Integer, Lrow As Integer

Lrow = Cells(Rows.count, "I").End(xlUp).Row
For Each rng In Range("I2:I" & Lrow)
  If rng.Offset(1, 0) - rng > 1 Then
    For i = 1 To (rng.Offset(1, 0) - rng) - 1
       rng.Offset(i, 0).EntireRow.Insert Shift:=xlDown
       rng.Offset(i, 0) = rng + i
     Next
  End If
Next

End Sub
This would be the result I want - did this work on the original mini sheet I provided?
How do I adjust it for my actual sheet the mini sheet provided in my last post?

when I run it as is - I get an error. The debugger goes to "If rng.Offset(1,0)-rng>1 then
When I hover over rng.offset it says 9/26/22 and rng>1 says "First Payment due date"
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,773
Members
449,336
Latest member
p17tootie

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