Macro to archive and delete data question

2001sleeper

New Member
Joined
Aug 16, 2017
Messages
3
Hello,
As many other posts I am a beginner and not sure where to start.
I have a spreadsheet for process pH values that is setup as follows:

Date8/1/20178/2/20178/3/20178/4/2017
PHPHPHPHPH
Sump17.857.527.467.38
Sump27.547.487.207.40
Sump38.038.427.537.50

<colgroup><col><col span="4"></colgroup><tbody>
</tbody>

Row 1 is where the dates are found and I am wanting to have a macro that will only keep TODAY()-7 days. So basically, every day I open the spreadsheet OR manually run the macro, all columns older than 7 days will be cut and relocated to an Archive spreadsheet and the empty columns will be deleted allowing the data to shift left.

Is this possible?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Yes, with excel you can run macro's when the worksheet opens, closes or saves. You'd just set a Workbooks.Open routine to run your code and dump the columns to your archive worksheet. Is the Archive part of your current workbook or a separate workbook, either way both are possible it's just obviously easier to dump the data in a separate worksheet within the workbook than opening another workbook to write to
 
Upvote 0
You could try something like this code to get you started, just copy and save it under the thisWorkbook object in the vba editor, it should run the code when you open the file, other options are to run it before closing or before saving automatically. It's assuming the sheets are called Data and Archive as I've no idea what you've called them, the archive data is inserted in column A to try and keep it consecutive
Code:
Private Sub Workbook_Open()
Dim LastCellData As Integer, LastColumnData As Integer, LastColumnArchive As Integer, i As Integer
Application.ScreenUpdating = False
Application.Calculation = xlCalculateManual
On Error GoTo xit:

LastColumnData = Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column

For i = LastColumnData To 2 Step -1

If DateDiff("d", Sheets("Data").Cells(1, i).Value, Now()) > 7 Then
LastColumnArchive = Sheets("Archive").Cells(1, Columns.Count).End(xlToLeft).Column
LastCellData = Sheets("Data").Cells(Rows.Count, i).End(xlUp).Row
Sheets("Archive").Range("A1").EntireColumn.Insert


Sheets("Archive").Range(Sheets("Archive").Cells(1, 1), Sheets("Archive").Cells(LastCellData, 1)).Value = _
Sheets("Data").Range(Sheets("Data").Cells(1, i), Sheets("Data").Cells(LastCellData, i)).Value


Sheets("Data").Columns(i).EntireColumn.Delete

End If
Next i

xit:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Last edited:
Upvote 0
Great!
I will give it try tomorrow and play around with it.

Instead of inserting the data on the archive worksheet, is just adding to the columns to the end possible?
 
Upvote 0
If you want them at the end try this, it should hopefully retain the order too

Code:
Private Sub Workbook_Open()
Dim LastCellData As Integer, LastColumnData As Integer, LastColumnArchive As Integer, i As Integer
Application.ScreenUpdating = False
Application.Calculation = xlCalculateManual
On Error GoTo xit:


LastColumnData = Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column


For i = LastColumnData To 2 Step -1


If DateDiff("d", Sheets("Data").Cells(1, i).Value, Now()) > 7 Then
LastColumnArchive = Sheets("Archive").Cells(1, Columns.Count).End(xlToLeft).Column
LastCellData = Sheets("Data").Cells(Rows.Count, i).End(xlUp).Row






Sheets("Archive").Range(Sheets("Archive").Cells(1, LastColumnArchive + 1), Sheets("Archive").Cells(LastCellData, LastColumnArchive + 1)).Value = _
Sheets("Data").Range(Sheets("Data").Cells(1, i), Sheets("Data").Cells(LastCellData, i)).Value




Sheets("Data").Columns(i).EntireColumn.Delete




End If
Next i


xit:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,381
Messages
6,136,228
Members
450,000
Latest member
jgp19

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