Consulting

Results 1 to 16 of 16

Thread: VBA - Split time range in 10 seconds increments

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location

    VBA - Split time range in 10 seconds increments

    Hi i need some help with the following issue:

    In Column A i have a start date & time and in Column B my end date & time. I would like to split these intervals into 10 second increments in Column D (see attached screenshot)



    Any suggestions would be appreciated.

    Thanks

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

    A1 = Start Date
    B1 = End Date

    [vba]
    Sub AddSecs()

    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).Clear
    Range("D1") = Range("A1")
    i = 2
    Do
    Range("D" & i) = DateAdd("s", 10, Range("D" & i - 1))
    If Range("D" & i) = Range("B1") Then Exit Do
    i = i + 1
    Loop
    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).NumberFormat = "dd.mm.yyyy hh:mm:ss"
    End Sub
    [/vba]
    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
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    In D2: 01-01-2013 00:00:00
    In D3: 01-01-2013 00:00:10

    Select D2:D3;
    drag down using the handle.

  4. #4
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    Quote Originally Posted by mancubus
    hi.

    A1 = Start Date
    B1 = End Date

    [vba]
    Sub AddSecs()

    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).Clear
    Range("D1") = Range("A1")
    i = 2
    Do
    Range("D" & i) = DateAdd("s", 10, Range("D" & i - 1))
    If Range("D" & i) = Range("B1") Then Exit Do
    i = i + 1
    Loop
    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).NumberFormat = "dd.mm.yyyy hh:mm:ss"
    End Sub
    [/vba]
    Hi thanks for the answer but unfortunately it doesn't work. First it loops until the last row in excel is reached and second i doesn't take the exact start and end time as in Column A & B.


  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i cant duplicate the problem. i tested the procedure before posting here.

    see the attached file.

    this line is added to prevent an endless loop:
    [vba]
    If Range("D" & i) = Range("B1") Then Exit Do
    [/vba]

    loop does not end means the code cannot test the condition. regional settings maybe. i dont know.
    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)

  6. #6
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    Thanks mate, you were of great help. I don't now what i messed up the first time but now it work great. However there is still one more issue i need to figure out. As you can see from my first screenshot A3 & B3 define a new Time range which should be filled just as the previous one after the first range (A2-B2) ends. I marked it green. Do you have a solution for that.
    I have many Time ranges just as these and i want to have a loop that goes through all of them and splits each in 10s increments just as you did for the first one.

    I really appreciate your effort.
    Bye

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i cannot see any attachment. probably because of internet filters.

    can you attach your file.

    click Go Advanced, scroll down, click Manage Attachments... to upload a file...
    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)

  8. #8
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    Quote Originally Posted by mancubus
    i cannot see any attachment. probably because of internet filters.

    can you attach your file.

    click Go Advanced, scroll down, click Manage Attachments... to upload a file...
    i modified Sheet1 in your file. I added a new time range and colored the cells yellow where the splited results should be filled
    Attached Files Attached Files

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    [VBA]
    Sub AddSecsMulti()
    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).Clear

    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    Range("D" & Rows.Count).End(xlUp).Offset(1) = Range("A" & i)
    Do
    With Range("D" & Rows.Count).End(xlUp)
    .Offset(1) = DateAdd("s", 10, .Value)
    If .Value = Range("B" & i) Then Exit Do
    End With
    Loop
    Next i

    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).NumberFormat = "dd.mm.yyyy hh:mm:ss"
    End Sub
    [/VBA]
    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)

  10. #10
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    Quote Originally Posted by mancubus
    [vba]
    Sub AddSecsMulti()
    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).Clear

    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    Range("D" & Rows.Count).End(xlUp).Offset(1) = Range("A" & i)
    Do
    With Range("D" & Rows.Count).End(xlUp)
    .Offset(1) = DateAdd("s", 10, .Value)
    If .Value = Range("B" & i) Then Exit Do
    End With
    Loop
    Next i

    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).NumberFormat = "dd.mm.yyyy hh:mm:ss"
    End Sub
    [/vba]
    Works almost perfectly...just that the loop should end i step earlier, since i get one additional 10 secs increment added to each time range (see attached)
    Attached Files Attached Files

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

    just a quick fix:
    change the if statement to
    [VBA]
    If .Value = Range("B" & i) Then .Offset(1).Clear: Exit Do
    [/VBA]
    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)

  12. #12
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    Quote Originally Posted by mancubus
    sorry. my bad.

    just a quick fix:
    change the if statement to
    [vba]
    If .Value = Range("B" & i) Then .Offset(1).Clear: Exit Do
    [/vba]
    Thanks for everything....works great. Have a nice day and sorry for keeping you busy with this

    Bye

  13. #13
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are most welcome.

    i glad i could help.
    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)

  14. #14
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    One more question mancubus,
    if i have an additional column containing information about my time ranges,
    is there an option to copy it next to my splitted time. I attached your file, and i manually did the copying so that you see what i mean.

    Thanks
    Attached Files Attached Files

  15. #15
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    [vba]
    Sub AddSecsMulti()
    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).Clear

    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    Range("D" & Rows.Count).End(xlUp).Offset(1) = Range("A" & i)
    Do
    With Range("D" & Rows.Count).End(xlUp)
    .Offset(1, 0) = DateAdd("s", 10, .Value)
    .Offset(0, 1) = Range("C" & i)
    If .Value = Range("B" & i) Then .Offset(1).Clear: Exit Do
    End With
    Loop
    Next i

    Range("D1" & Range("D" & Rows.Count).End(xlUp).Row).NumberFormat = "dd.mm.yyyy hh:mm:ss"
    End Sub
    [/vba]
    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)

  16. #16
    VBAX Regular
    Joined
    Jul 2008
    Posts
    51
    Location
    i just wanted to tell you that i got this by myself..i figured out the role of .offset )) thanks anyway

Posting Permissions

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