Automatically remove oldest record when a new record is added

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
235
Office Version
  1. 365
Platform
  1. Windows
I am considering using a table (or simple worksheet) in Excel to maintain records of the hours worked by a small group of employees over a period of 52 weeks. I will be adding one new line of records each week and I am looking for a way to remove the first row of information (oldest) as each new row of information is added. I will then use this to calculate the average number of hours worked by each employee based on the number of hours / actual weeks they worked during this period.

I can manage the calculations but would welcome assistance with either VBA Code or a Macro that will maintain just 52 rows of information in a table.

Thank you in advance
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Assuming that you use a formal Excel table and that it is the only formal table on the worksheet. You could try this Worksheet_Change event macro. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.
4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  With ActiveSheet.ListObjects(1)
    If Not Intersect(Target, .DataBodyRange) Is Nothing Then
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Do Until .DataBodyRange.Rows.Count <= 52
        .ListRows(1).Delete
      Loop
      Application.EnableEvents = True
      Application.ScreenUpdating = True
    End If
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,937
Messages
6,175,513
Members
452,650
Latest member
Tinfish

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