Hi
I have the following VBA, please see below the code and the attachment:
Option Explicit
Dim a As Long, b As Long, c As Long, d As Long, m As Long, n As Long, r As Long
Dim date1 As Object, date2 As Object
Sub addrows()
With Sheet1
Dim LstRw As Long, Rng As Range, List As Object
LstRw = Cells(Rows.Count, "A").End(xlUp).Row
Set List = CreateObject("Scripting.Dictionary")
For Each Rng In Range("A2:A" & LstRw)
If Not List.Exists(Rng.Value) Then List.Add Rng.Value, Nothing
Next
b = List.Count
.Range("A2").Select
For c = 1 To b
r = ActiveCell.Row
If .Cells(r, 4) > .Cells(r, 7) Then
Set date1 = .Cells(r, 7)
Set date2 = .Cells(r, 4)
'Calculate the difference in months between two dates
d = DateDiff("m", date1, date2)
For n = 1 To d
ActiveCell.Offset(n, 0).EntireRow.Insert
.Range(.Cells(r, 1), .Cells(r, 9)).Copy Destination:=.Cells(r + n, 1)
.Cells(r + n, 3).ClearContents
If .Cells(r + n - 1, 3) = 1 Then
.Cells(r + n, 3) = 12
.Cells(r + n, 2) = .Cells(r + n - 1, 2) - 1
Else: .Cells(r + n, 3) = .Cells(r + n - 1, 3) - 1
.Cells(r + n, 2) = .Cells(r + n - 1, 2)
End If
Next
End If
Rows(r + d + 1).Select
If .Cells(r + d + 1, 1) = "" Then Exit Sub
Next
End With
End Sub
The above code used to work for the following input: Original_Input.JPG
Attachment 724316
That was providing this output:
Attachment 724318
Problem is that now I have this input:
Attachment 724317
And I am expecting the same output as for the original input picture, such as decreasing the date until they are the same (until end date decreases so much that
= start date) whilst copying the rows for each iteration.
Any help would be greatly appreciated!
Gordon
Example of an expected output:
Bookmarks