Results 1 to 18 of 18

Thread: [RESOLVED] [EXCEL] Find then subtract

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Resolved [RESOLVED] [EXCEL] Find then subtract

    I have this data on a worksheet

    Name:  vb.bmp
Views: 167
Size:  461.1 KB

    I want to be able to calculate the elapsed time per status change (i.e. from Open to the last Assigned To, from the last Assigned To to the last Pending Testing) which can be output on a new column, new worksheet, wherever as long as the output will look like this (this is in a new worksheet):

    Name:  vb2.bmp
Views: 159
Size:  438.8 KB

    I need a code since there may be a lot of records and I need to be able to do it with just a simple push of a button.

    Help?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    you can try like this
    vb Code:
    1. mystr = Range("c2")
    2. r = 2
    3. For Each c In Range("C3:c99")  ' change to last row +1
    4.   If Not c = mystr Then curstr = c: nex = c.Row
    5.   If Not c = curstr Then
    6.     etime = DateDiff("s", Cells(c.Row - 1, 3), Cells(r, 3))
    7.     edays = DateDiff("d", Cells(c.Row - 1, 3), Cells(r, 3))
    8.     ehours = edays = DateDiff("h", Cells(c.Row - 1, 3), Cells(r, 3)) - edays * 24
    9.     emins = DateDiff("n", Cells(c.Row - 1, 3), Cells(r, 3)) - (edays * 24 + ehours) * 60
    10.     esec = etime - ((edays * 24 + ehours) * 60 + emins) * 60
    11.     Cells(c.Row - 1, 4) = edays
    12.     Cells(c.Row - 1, 5) = ehours
    13.     Cells(c.Row - 1, 5) = emins
    14.     Cells(c.Row - 1, 7) = esec
    15.     r = nex
    16.     mystr = curstr
    17.   End If
    18. Next
    you could put formula in the cells instead of calculating the times, i have not tested this for correct calculation or looping, but it should basically work, you can either copy all to new sheet first then run the code, or incorporate coping each row to new sheet as you go
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    Thanks westconn1! But it didn't work I even changed C2 to B2 and the range to B - thinking that this might be comparing the status changes. Am I right? But still didn't do anything.

    Where will it output as per the code?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    I even changed C2 to B2 and the range to B - thinking that this might be comparing the status changes
    that would have been correct
    and line 13 should have been column 6
    can you post a sample workbook to test with?
    (save as .xls and zip)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    Here's a workbook for testing. I've already attached the code in VB Editor and tried it but didn't do any change.

    I really appreciate your help

    vb.zip

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    i fixed the code so it now works, but seconds can not be evaluated correctly (and cause an error in calculation) as they are not included in the date /time stamp

    check all results carefully
    vb Code:
    1. mystr = Range("B3")
    2. r = 2
    3. lastrow = Range("b65535").End(xlUp).Row
    4. For Each c In Range("B3:B" & lastrow + 1) ' change to last row +1
    5.    
    6.     If Not c = mystr Then
    7.         etime = DateDiff("n", Cells(r, 3), Cells(c.Row - 1, 3))
    8.         ehrs = etime \ 60
    9.         emins = etime - ehrs * 60
    10.         edays = ehrs \ 24
    11.         ehrs = ehrs - edays * 24
    12.         Cells(c.Row - 1, 4) = edays
    13.         Cells(c.Row - 1, 5) = ehrs
    14.         Cells(c.Row - 1, 6) = emins
    15.         Cells(c.Row - 1, 7) = esec
    16.         r = nex
    17.         mystr = c
    18.         Else
    19.             nex = c.Row
    20.     End If
    21. Next
    tested in your sample sheet
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [EXCEL] Find then subtract

    The use of DateDiff() is complicated in this case.
    Code:
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim Status  As String
        Dim dStart  As Date
        Dim Elapsed As Date
        Dim r       As Long
        
        Status = Cells(2, 2)
        dStart = Cells(2, 3)
        r = 3
        Do While Cells(r, 2) <> ""
            If Cells(r, 2) <> Status Then
                Elapsed = Cells(r, 3) - dStart
                Cells(r, 4) = Int(Elapsed) '-- extract days as the integer part of Elapsed
                                           '-- cannot use Day(Elapsed), it's wrong
                Cells(r, 5) = Hour(Elapsed)
                Cells(r, 6) = Minute(Elapsed)
                Cells(r, 7) = Second(Elapsed)
                Status = Cells(r, 2)
                dStart = Cells(r, 3)
            Else
                Cells(r, 4).Resize(, 4).ClearContents
            End If
            r = r + 1
        Loop
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    I modified westconn1's code to a little bit.

    The results display correctly (every last change of status - the last "assigned to", the last "pending", etc.) but now all I get are 0s as results.

    Here's the modified code

    vb Code:
    1. Private Sub CommandButton2_Click()
    2.  
    3. mystr = Workbooks("report.xls").Worksheets("Sheet2").Range("B3")
    4.  
    5. r = 2
    6.  
    7. lastrow = Workbooks("report.xls").Worksheets("Sheet2").Range("b65535").End(xlUp).Row
    8.  
    9. For Each c In Workbooks("report.xls").Worksheets("Sheet2").Range("B3:B" & lastrow + 1) ' change to last row +1
    10.  
    11.     date1 = Workbooks("report.xls").Worksheets("Sheet2").Cells(r, 3).Value
    12.     date2 = Cells(c.Row - 1, 3).Value
    13.  
    14.  
    15.     If Not c = mystr Then
    16.     etime = DateDiff("n", Cells(r, 3), Cells(c.Row - 1, 3))
    17.     ehrs = etime \ 60
    18.     emins = etime - ehrs * 60
    19.     edays = ehrs \ 24
    20.     esec = ehrs - edays * 24
    21.     Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 4) = edays
    22.     Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 5) = ehrs
    23.     Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 6) = emins
    24.     Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 7) = esec
    25.     r = nex
    26.     mystr = c
    27.     Else
    28.         nex = c.Row
    29.     End If
    30.    
    31. Next
    32.  
    33.  
    34. End Sub

    Thanks for your help westconn1 and anhn!

    I tried anhn's too, but the results for his shows up on the first instance of change of status.

  9. #9
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    i ran the code as posted above (#8) against the workbook you posted previously, with correct results
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    What if my data are in another sheet? I think this is why I'm getting all 0s as a result. The datediff isn't working properly.

    I dim date1 and date 2 to replace

    etime = DateDiff("n", Cells(r, 3), Cells(c.Row - 1, 3))

    Is there any way to do this?

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    if the data is in another sheet you must specify the sheet by name or index, or use a steet object
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    Here's what I did but not working:

    Code:
        date1 = Workbooks("report.xls").Worksheets("Sheet2").Cells(r, 3).Value
        date2 = Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 3).Value
        
        If Not c = mystr Then
        etime = DateDiff("n", Cells(r, 3), Cells(c.Row - 1, 3))

  13. #13
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    you assign values to date1 and date2, but you do not use then in the above example

    etime is calculated using values in the activesheet

    i would set sheet objects for each sheet then work with the object
    vb Code:
    1. set sht1 = Workbooks("report.xls").Worksheets("Sheet2")
    2. etime = DateDiff("n", sht1.Cells(r, 3), sht1.Cells(c.Row - 1, 3))
    in this the values are taken from a specific sheet

    but as c is a range object it is always on the sheet that is looped through, which may not be the same sheet
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  14. #14
    New Member
    Join Date
    Jun 2010
    Posts
    4

    Re: [EXCEL] Find then subtract

    vb Code:
    1. Range("D3:G3").Select
    2.     Selection.Copy
    3.     Range("D4:G8").Select
    4.     Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    5.         SkipBlanks:=False, Transpose:=False
    6.     Range("D4").Select

    for this snippet you need to give just two inputs one is the formula range and the other one is the data range

  15. #15

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    @westconn1 : that's what I originally did. But I get a "Type mismatch" error.

    As for the code i provided, it should've been like this:

    Code:
        date1 = Workbooks("report.xls").Worksheets("Sheet2").Cells(r, 3).Value
        date2 = Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 3).Value
        
        If Not c = mystr Then
        etime = DateDiff("n", date1, date2)
    But even that one results to an error.

    Here's how it looks like now (and I get the error):

    Code:
    Private Sub CommandButton2_Click()
    
    
    mystr = Workbooks("report.xls").Worksheets("Sheet2").Range("B3")
    
    r = 2
    
    lastrow = Workbooks("report.xls").Worksheets("Sheet2").Range("b65535").End(xlUp).Row
    
    Set sht1 = Workbooks("report.xls").Worksheets("Sheet2")
    
    For Each c In Workbooks("report.xls").Worksheets("Sheet2").Range("B3:B" & lastrow + 1)
        
        If Not c = mystr Then
        etime = DateDiff("n", sht1.Cells(r, 3), sht1.Cells(c.Row - 1, 3))
        ehrs = etime \ 60
        emins = etime - ehrs * 60
        edays = ehrs \ 24
        ehrs = ehrs - edays * 24
        
        Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 4) = edays
        Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 5) = ehrs
        Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 6) = emins
        Workbooks("report.xls").Worksheets("Sheet2").Cells(c.Row - 1, 7) = esec
        
        r = nex
        mystr = c
        
        Else
            nex = c.Row
        End If
        
    Next
    
     
    End Sub
    Last edited by the14th; Jul 7th, 2010 at 11:49 AM.

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Find then subtract

    Here's how it looks like now (and I get the error):
    which line?

    i ran the code you posted in #15, just changed workbook and sheet names to suit
    no errors, but i did not check the results

    you can change the code to fully use the sht1 object
    vb Code:
    1. Set sht1 = Workbooks("vb.xls").Worksheets("Sheet1")
    2. mystr = sht1.Range("B3")
    3. r = 2
    4. lastrow = sht1.Range("b65535").End(xlUp).Row
    5.  
    6. For Each c In sht1.Range("B3:B" & lastrow + 1)
    7.    
    8.     If Not c = mystr Then
    9.     etime = DateDiff("n", sht1.Cells(r, 3), sht1.Cells(c.Row - 1, 3))
    10.     ehrs = etime \ 60
    11.     emins = etime - ehrs * 60
    12.     edays = ehrs \ 24
    13.     ehrs = ehrs - edays * 24
    14.     sht1.Cells(c.Row - 1, 4) = edays
    15.     sht1.Cells(c.Row - 1, 5) = ehrs
    16.     sht1.Cells(c.Row - 1, 6) = emins
    17.     sht1.Cells(c.Row - 1, 7) = esec
    18.     r = nex
    19.     mystr = c
    20.    
    21.     Else
    22.         nex = c.Row
    23.     End If
    24. Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    I think it's working okay now. Except the results sometimes are incorrect.

    Last question, whats "nex" in line 18 and 22. I wasn't able to dim it and i think that's what's causing the incorrect values.

  18. #18

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    14

    Re: [EXCEL] Find then subtract

    Oops, nevermind. Solved it! Thank you so much for your help and patience, westconn1!

Posting Permissions

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



Click Here to Expand Forum to Full Width