VBA coding for Row Deletion

aggierichie

New Member
Joined
Mar 25, 2011
Messages
9
This should be a fairly easy one but I cannot seem to nail it down.

I have a simple 7 column sheet. This sheet feeds into other workbooks but is itself only a 1 sheet workbook.

What i need to happen is for the rows to auto delete when the dates expire. So, if an employee is returning to work on 3/15 (and placed his return date in Column D) then excel will automatically DELETE the corresponding Row on 3/15.

In Short. If the date in column D is todays date or later, Delete the corresponding row.

IF someone could help me out with the code on this I would be forever in your debt. I have tried several codes i have found on here but have not been able to get it to work correctly.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
This should be a fairly easy one but I cannot seem to nail it down.

I have a simple 7 column sheet. This sheet feeds into other workbooks but is itself only a 1 sheet workbook.

What i need to happen is for the rows to auto delete when the dates expire. So, if an employee is returning to work on 3/15 (and placed his return date in Column D) then excel will automatically DELETE the corresponding Row on 3/15.

In Short. If the date in column D is todays date or later, Delete the corresponding row.

IF someone could help me out with the code on this I would be forever in your debt. I have tried several codes i have found on here but have not been able to get it to work correctly.

Do you mean something like this?

Code:
Sub aggierichie()
'First in a separate cell I'd put for example A1 =Today()
Dim cl As Range
Dim lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row

For Each cl In Range("D2:D" & lr)

    If cl.Value >= Range("A1") Then
    
        cl.EntireRow.Delete
        
    End If
    
Next
    


End Sub
 
Upvote 0
If you wanted it to automatically happen you could put in in the worksheet_selection change like this:
Code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lastrow As Long, icell As Long
lastrow = Range("D" & Rows.Count).End(xlUp).Row
For icell = 1 To lastrow
        If Range("D" & icell).Value <= Date Then
           Range("A" & icell).EntireRow.Delete Shift:=xlUp
        End If
Next icell
End Sub

Dont need to put =Today() in A1 for this.
 
Upvote 0
John Davis, Thanks for the reply but I wanted it to automatically happen and I cant seem to get this one to automatically go

stnkynts- I would like it to happen automatically but i am not 100 pct sure where to put in that formula. I am not super proficient with VBA but am teaching myself as i go. Sorry to be a pain but could you let me know where to put that and ill drop it in.
 
Upvote 0
If you are using excel 2007 gain access to the developer tab and click Visual Basic. Or just hit Alt +F11.

Right click on whichever sheet has your "7 columns" in it. Click View Code. Paste the code i provided there.
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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