VBA Macro Help

Anonnymouse

New Member
Joined
Jun 28, 2023
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Hello,

I was wondering if its possible to create a macro that deletes cells based on date.
For example I would like it to delete cell d3 and e3 since it has been longer than a year and also would like to delete column f4 and g4 and also h4 and i4.

When its deleted is there a way to make it so it doesn't shift the cells in column k,l, and M since they have formulas and it would then break the formulas.

ABCDEFGHIJKLM
1
2Number of timesTomMichaelTim
3Tom6/4/2023Michael8/1/2024Tim9/1/2023111
4Michael4/1/2024Michael1/2/2023Tom1/1/2023120
5Tim4/6/2024Tim4/6/2024Michael4/6/2024012
6
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this:
VBA Code:
Sub MyClearValues()

    Dim c As Long
    Dim r As Long
    Dim lr As Long
    
    Application.ScreenUpdating = False
    
'   Loop through columns E, G, and I
    For c = 5 To 9 Step 2
'       Find last row with data in current column
        lr = Cells(Rows.Count, c).End(xlUp).Row
'       Loop through all row starting in row 3
        For r = 3 To lr
'           See if date in current cell is more than a year old
            If Cells(r, c).Value <= DateAdd("m", -12, Date) Then
'               Clear current cell and cell to the left
                Range(Cells(r, c - 1), Cells(r, c)).ClearContents
            End If
        Next r
    Next c
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!", vbOKOnly
    
End Sub
 
Upvote 0
Another option:
VBA Code:
Private Sub ClearDates()
Dim c As Range
For Each c In Range("C3:H5")
    If c.Value < Date - 365 Then
        c.ClearContents
        c.Offset(0, -1).ClearContents
    End If
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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