Insert row based on months.

wikus

Active Member
Joined
May 2, 2010
Messages
320
Office Version
  1. 365
Any help will be appreciated.
I want to insert empty rows based on the amount of months between start and end date.
4 empty rows after first date pair.
5 empty rows after second date pair.

1704798948562.png


1704798835619.png
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hello,

This should work for you:
VBA Code:
Sub test()
  Dim lRow As Long, i As Long, j As Long
  lRow = Cells(Rows.Count, "A").End(xlUp).Row
  With Application
  .ScreenUpdating = False
  For i = lRow To 2 Step -1
    For j = 1 To Evaluate("Mod(" & Month(Cells(i, "B").Value) - Month(Cells(i, "A").Value) & ",12)")
      Rows(i).Offset(1).EntireRow.Insert
    Next
  Next
  .ScreenUpdating = True
  End With
End Sub
 
Last edited by a moderator:
Upvote 0
See if you can get this macro to work on your project (paste it in a standard or in the sheet's module):
VBA Code:
Option Explicit
Sub InsertRows()
    Dim LastRow As Long
    Dim AddToRow As Long
    Dim InsDiff As Integer
    LastRow = Range("A" & Rows.Count).End(xlUp).Row 'calculate last row of column A
    For AddToRow = LastRow To 2 Step -1           'from last to second row
        InsDiff = DateDiff("m", Range("A" & AddToRow), Range("B" & AddToRow)) 'calculate difference by months
        If InsDiff > 1 Then Range("A" & AddToRow + 1 & ":A" & AddToRow + InsDiff).EntireRow.Insert 'insert rows if difference
    Next AddToRow
End Sub
 
Upvote 0
Solution
See if you can get this macro to work on your project (paste it in a standard or in the sheet's module):
VBA Code:
Option Explicit
Sub InsertRows()
    Dim LastRow As Long
    Dim AddToRow As Long
    Dim InsDiff As Integer
    LastRow = Range("A" & Rows.Count).End(xlUp).Row 'calculate last row of column A
    For AddToRow = LastRow To 2 Step -1           'from last to second row
        InsDiff = DateDiff("m", Range("A" & AddToRow), Range("B" & AddToRow)) 'calculate difference by months
        If InsDiff > 1 Then Range("A" & AddToRow + 1 & ":A" & AddToRow + InsDiff).EntireRow.Insert 'insert rows if difference
    Next AddToRow
End Sub
Thank you very much for the reply.
 
Upvote 0
Thanks for the positive feedback(y), glad we were able to help.
 
Upvote 0
Thank you very much for the reply.
Hi Rollis,
I am trying to expand on your code to copy the row into the newly create blank rows but it's not working. May I ask for your assistance?
If I'm not allowed to ask more questions regarding this issue I apologize.
Thank you
Wikus
 
Upvote 0
In truth, it seems to me that it is actually a different request from the title of this thread so in these cases, in order to have even a minimum of visibility in the Forum, I suggest starting a new thread with a new appropriate title.
 
Upvote 0
In truth, it seems to me that it is actually a different request from the title of this thread so in these cases, in order to have even a minimum of visibility in the Forum, I suggest starting a new thread with a new appropriate title.
Will do, thank you.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,270
Members
452,902
Latest member
Knuddeluff

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