Consulting

Results 1 to 13 of 13

Thread: Find earliest and latest date in ranges with blanks

  1. #1

    Find earliest and latest date in ranges with blanks

    As part of a larger vba project, I need to find the earliest date in a range and subtract from the latest date in another range to get the length of time (number of days). In addition, the range with the earliest date has blanks. Everything I've tried so far ends up with a 0. I know dates are tricky, but for the life of me I can't figure it out. I've tried min and max and mina and maxa, to no avail. I'd appreciate any pushes in the right direction.

    thank you,

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    try this.

    Sub MaxMinDateDiff()
    
    
        Dim minD As Long, maxD As Long, diffD As Long
        Dim Rng As Range
        
        Set Rng = Range("A1:C5000") 'Change to suit
        
        minD = Evaluate("=MIN(IF(" & Rng.Address & ">0," & Rng.Address & "))")
        maxD = Application.Max(Rng)
        diffD = maxD - minD
        
        MsgBox diffD
    
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thank you for the reply, however, I still get a 0 for the min and max values. I cannot post the entire file as it has confidential information, so I have copied the two columns I am trying to get the information from. I'm also including the relevant portion of the code I'm attempting so the ranges won't match the excel file but I think you'll get the gist. Thanks again for the help.


    'Calculate LTS
    Set rLocation = Range("Z2:Z" & Range("A" & Rows.Count).End(xlUp).Row)
    For Each c In rLocation
        c.Value = "=round(RC[-6]-RC[-8],0)"
        c.NumberFormat = "0"
        If IsError(c) = True Then
        c.Value = "0"
        End If
        c.Offset(0, -1).Value = "=RC[-4]-RC[-5]"
        c.Offset(0, -1).NumberFormat = "[h]:mm"
            Next c
    Select Case Application.CountIf(rLocation, 0)
    Case Is = rLocation.Cells.Count: strLTS = "not calculated"
    Case 0: strLTS = Application.Max(rLocation)
    Case Is < rLocation.Cells.Count: minD = Evaluate("=MIN(IF(" & Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).Address & ">0," & Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).Address & "))")
        maxD = Application.Max(Range("T2:T" & Range("A" & Rows.Count).End(xlUp).Row))
        strLTS = maxD - minD
    End Select

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome. i think columns R, T, and U contain dates and blank cells. try this.
    Sub ddiff_vba()    
    Dim LR As Long, minD As Long, maxD As Long, strLTS As Long    
    Dim rLocation As Range        
    LR = Range("A" & Rows.Count).End(xlUp).Row    
    Set rLocation = Range("Z2:Z" & LR)        
    With rLocation        
    .FormulaR1C1 = "=IFERROR(ROUND(RC[-6]-RC[-8],0),0)"        
    .NumberFormat = "0"        
    .Offset(0, -1).FormulaR1C1 = "=RC[-4]-RC[-5]"        
    .Offset(0, -1).NumberFormat = "[h]:mm"    
    End With        
    Select Case Application.CountIf(rLocation, 0)        
    Case Is = rLocation.Cells.Count            
    strLTS = "not calculated"        
    Case 0            
    strLTS = Application.Max(rLocation)        
    Case Is < rLocation.Cells.Count            
    minD = Evaluate("=MIN(IF(" & Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).Address & ">0," & Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).Address & "))")            
    maxD = Application.Max(Range("T2:T" & Range("A" & Rows.Count).End(xlUp).Row))            
    strLTS = maxD - minD    
    End Select        
    MsgBox "MinDate: " & minD & " or " & Format(minD, "mm.dd.yyyy")    
    MsgBox "MaxDate: " & maxD & " or " & Format(maxD, "mm.dd.yyyy")    
    MsgBox "DaysDiff: " & strLTS
    End Sub
    Last edited by Aussiebear; 11-06-2013 at 01:02 PM. Reason: I think I got it right
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i cant upload a file in my office computer nor can i paste the codes here with line feeds. ---- also I (or VBAX) cant upload a file from dropbox or skydrive using mobile/smart phone.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    finallay attached a sample file

    Sub ddiff_vba()
    Dim LR As Long, minD As Long, maxD As Long, strLTS As Long
        Dim rLocation As Range
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Set rLocation = Range("Z2:Z" & LR)
        With rLocation
            .FormulaR1C1 = "=IFERROR(ROUND(RC[-6]-RC[-8],0),0)"
            .NumberFormat = "0"
            .Offset(0, -1).FormulaR1C1 = "=RC[-4]-RC[-5]"
            .Offset(0, -1).NumberFormat = "[h]:mm"
        End With
        Select Case Application.CountIf(rLocation, 0)
            Case Is = rLocation.Cells.Count
                strLTS = "not calculated"
            Case 0
                strLTS = Application.Max(rLocation)
            Case Is < rLocation.Cells.Count
                minD = Evaluate("=MIN(IF(" & Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).Address & ">0," & Range("R2:R" & Range("A" & Rows.Count).End(xlUp).Row).Address & "))")
                maxD = Application.Max(Range("T2:T" & Range("A" & Rows.Count).End(xlUp).Row))
                strLTS = maxD - minD
        End Select
        MsgBox "MinDate: " & minD & " or " & Format(minD, "mm.dd.yyyy")
        MsgBox "MaxDate: " & maxD & " or " & Format(maxD, "mm.dd.yyyy")
        MsgBox "DaysDiff: " & strLTS
    End Sub
    Attached Files Attached Files
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    When I use your example sheet, it works. However, when I plug in my dates, I still get "0".

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    perhaps seeing your file may help diagnose the problems.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  9. #9
    Sorry, I thought I had attached an example with my last post, but I can't seem to be able to attach a file right now.

  10. #10
    Sub M_snb()
       x = [max(if(T2:T100="",0,if(R2:R100="",0,T2:T100-R2:R100)))] & " days"
       y = [min(if(T2:T100="",today(),if(R2:R100="",today(),abs(T2:T100-R2:R100))))] & " days"
    End Sub

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,091
    Location
    Is the formatting of the cells the same?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    I finally could upload/attach a file. It is really just the dates because the rest is confidential, but it will give you an idea of the formatting of the cells. I really appreciate everyone's help on this.
    Attached Files Attached Files

  13. #13
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    your table contains strings displayed as dates rather than dates. so you have to convert them to dates before running the macro.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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