Macro to Move Data and Delete Extra Rows

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have an Excel-based report that lists several people with a series of events listed in chronological order. I only need the start time of the first event and the end time of the final event for each individual. I do this manually by cutting that end time at the bottom of each series for each person and pasting it in the cell with the end time of the first event. The rest of the rows for each person are then deleted. I would like to code a macro that automates this process. Can anyone help?

This is basically what the report looks like initially:

Employee IDLast NameFirst NameBegin TimeEnd TimeSupervisor
user1SmithJohn5/28/2019 23:205/28/2019 23:21sup_1
user1SmithJohn5/28/2019 23:215/28/2019 23:25sup_1
user2DoeJane5/29/2019 0:205/29/2019 0:42sup_2
user2DoeJane5/29/2019 0:425/29/2019 1:26sup_2

<tbody>
</tbody>



Here is what it should end up looking like:

Employee IDLast NameFirst NameBegin TimeEnd TimeSupervisor
user1SmithJohn5/28/2019 23:205/28/2019 23:25sup_1
user2DoeJane5/28/2019 0:205/28/2019 1:26sup_2

<tbody>
</tbody>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Will the data always be sorted like this, so all the records for a particular user a group together in chronological order?
For a particular user, is it safe to assumes that the First Name, Last Name, and Supervisor fields will always be the same for all of the records for that user?
 
Upvote 0
Yes, the data will always be sorted chronologically per user, and for each user, First Name, Last Name, and Supervisor fields will be the same.
 
Upvote 0
If the data is in columns A-F, with header on row 1 and data starting on row 2, this should work:
Code:
Sub CombineRecords()

    Dim r As Long

    Application.ScreenUpdating = False
    
'   Loop through all rows, starting on row 2
    r = 2
    Do Until Cells(r, "A") = ""
'       If value is same as above, update end date and delete record
        If Cells(r, "A") = Cells(r - 1, "A") Then
            Cells(r - 1, "E") = Cells(r, "E")
            Rows(r).Delete
'       Otherwise move to next row
        Else
            r = r + 1
        End If
    Loop
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top