Results 1 to 3 of 3

Subtract dates until they are the same

Threaded View

  1. #1
    Registered User
    Join Date
    01-30-2020
    Location
    Planet Earth
    MS-Off Ver
    2016
    Posts
    96

    Subtract dates until they are the same

    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:
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Gordon85; 03-18-2021 at 05:15 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Subtract dates.
    By EnG_TeLeCoMm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-29-2017, 03:23 PM
  2. how to subtract dates with text in the
    By bell_man in forum Excel General
    Replies: 9
    Last Post: 03-17-2016, 06:20 PM
  3. [SOLVED] How to subtract the dates
    By uttam.mothe in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-06-2013, 08:43 AM
  4. Subtract dates in same column
    By jimde in forum Excel General
    Replies: 10
    Last Post: 01-01-2013, 09:35 PM
  5. Subtract dates d:hh:mm
    By Allardin in forum Excel General
    Replies: 14
    Last Post: 10-24-2008, 08:39 PM
  6. [SOLVED] Subtract two dates
    By Joe in forum Excel General
    Replies: 2
    Last Post: 02-07-2006, 11:10 PM
  7. How do you Subtract dates in VBA
    By postlp60 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2005, 06:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1