Macro to delete rows or fill them wiht N/A

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
The following will delete the entire row if the criteria is met. You will have to change the ranges to fit your needs. Be sure to test it on copy of your data first.

Code:
Sub DeleteEmptyRows()
    Dim rng As range
    
    Set rng = range(range("A2"), range("A65536").End(xlUp))

    rng.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
Code:
Sub DeleteRowAfterDate()
    Dim rng As range, cell As range
    Dim delRng As range
    Dim dateCol As String
    Dim cutoffDate As Date

    dateCol = "B"
    
    cutoffDate = DateValue("05/31/2000")
    
    Set rng = range(range(dateCol & "2"), range(dateCol & "65536").End(xlUp))
    
    For Each cell In rng
        If IsDate(cell.Value) And cell.Value > cutoffDate Then
            If delRng Is Nothing Then
            Set delRng = cell
            Else
            Set delRng = Union(delRng, cell)
            End If
        End If
    Next cell
    delRng.EntireRow.Delete
End Sub
Code:
Sub DeleteRowBeforeDate()
    Dim rng As range, cell As range
    Dim delRng As range
    Dim dateCol As String
    Dim cutoffDate As Date

    dateCol = "B"
    
    cutoffDate = DateValue("05/31/2000")
    
    Set rng = range(range(dateCol & "2"), range(dateCol & "65536").End(xlUp))
    
    For Each cell In rng
        If IsDate(cell.Value) And cell.Value < cutoffDate Then
            If delRng Is Nothing Then
            Set delRng = cell
            Else
            Set delRng = Union(delRng, cell)
            End If
        End If
    Next cell
    delRng.EntireRow.Delete
End Sub
 
Upvote 0
Thank you! That works Great! is there anyway to add a popup box to input the date for the range to delete After date?
 
Upvote 0
Thank you I got everything to work. but now I am getting this:

This is the code I have it is throwing out the highlighted as a fail.

Rich (BB code):
Sub DeleteRowAfterDate()
    Dim rng As Range, cell As Range
    Dim delRng As Range
    Dim dateCol As String
    Dim cutoffDate As Date
    Dim TheString As String
 
TheString = Application.InputBox("Enter A Date")
 
If IsDate(TheString) Then
    cutoffDate = DateValue(TheString)
Else
    MsgBox "Invalid date"
 
End If
 
    dateCol = "B"
        Set rng = Range(Range(dateCol & "2"), Range(dateCol & "65536").End(xlUp))
 
    For Each cell In rng
        If IsDate(cell.Value) And cell.Value > cutoffDate Then
            If delRng Is Nothing Then
            Set delRng = cell
            Else
            Set delRng = Union(delRng, cell)
            End If
        End If
    Next cell
    delRng.EntireRow.Delete
 
End Sub

The only thing I can think of is maybe there is an issue cropping up about defining delRng maybe? or Cell
 
Upvote 0
Has the code worked at all because without that line executing nothing will change on the sheet. I guess if there is nothing to delete it will error out. Try replacing that line with this
Code:
If Not delRng Is Nothing Then delRng.EntireRow.Delete

and let me know if you are stilling getting problems.
 
Upvote 0
What was the message that pops up when the code hits that line?
 
Last edited:
Upvote 0
Here are some improvements that will prompt user for date column to check and reprompt for date if it is not valid. Allowing for the macro to be a little more versatile.

Code:
Sub DeleteRowAfterDate()
    Dim Rng As range, cell As range
    Dim delRng As range, dateRng As range
    Dim dateCol As Integer
    Dim cutoffDate As Date
    Dim theString As String
    
    On Error Resume Next        '// Error handling for canceling the range inputbox
    
    '// Prompt user for column containing the date range to check
    Do
        Set dateRng = Application.InputBox( _
                            Prompt:="Select a cell in the Date Column", _
                            Title:="Date Column Select", _
                            Type:=8)
        '// Exit Sub if nothing was selected or the Cancel 'x' button was selected
        If dateRng Is Nothing Then Exit Sub
        
    '// Reprompt if more than one column was selected
    Loop While dateRng.Columns.Count > 1
    
    On Error GoTo 0             '// Resume default error handling
    
    dateCol = dateRng.Column
    
    theString = Application.InputBox("Enter A Date")
    '// Cancel 'x' button was selected then exit sub
    If theString = "False" Then Exit Sub
    
    Do While Not IsDate(theString)
        theString = Application.InputBox("Date Invalid! Enter a Valid Date")
        '// Cancel 'x' button was selected then exit sub
        If theString = "False" Then Exit Sub
    Loop
    
    cutoffDate = DateValue(theString)
    
    Set Rng = range(Cells(2, dateCol), Cells(65535, dateCol).End(xlUp))
    
    For Each cell In Rng
        If IsDate(cell.Value) And cell.Value > cutoffDate Then
            If delRng Is Nothing Then
            Set delRng = cell
            Else
            Set delRng = Union(delRng, cell)
            End If
        End If
    Next cell
    If Not delRng Is Nothing Then delRng.EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
rootytrip

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