VBA to delete criteria is deleting my first row regardless of criteria.

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks

I worked out a function and a procedure to help me find the last row and then delete filtered criteria. If there is not criteria, then leave the data alone. However, the procedure does filter and delete the data based on the criteria, but if there is no criteria instead of leaving the row alone, it deletes my first row.

Here is my function

Code:
Public Function GETLASTROW(ByVal rngToCheck As Range) As Long

'This function is used by all procedures where row values are deleted depeding
'on a certain value or combination of values.
'The function is called to determine the last populated row of a specified range:


Dim rngLast As Range
    
    Set rngLast = rngToCheck.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
    
        If rngLast Is Nothing Then
            GETLASTROW = rngToCheck.Row
        
                Else
                 GETLASTROW = rngLast.Row
                 
                    End If
                    
                    
    
End Function

Here is my procedure

Code:
Sub last_row()

Dim lngLastRow As Long


    Application.ScreenUpdating = False
    
    With Sheets("New Leaves Report")
        
        lngLastRow = GETLASTROW(.Cells)
        
        If lngLastRow > 1 Then
            'we don't want to delete our header row
            With .Range("B2:B" & lngLastRow)
            
                    .AutoFilter Field:=1, Criteria1:="#N/A"
                    .EntireRow.delete
                    
            End With
        End If
    End With
    
    Application.ScreenUpdating = True
    
    
End Sub

I can't figure out why is deleting row B2 even if there is no criteria to delete. Any help is appreciated.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
The filter needs to go in the header row.
Is column B formulae?
 
Upvote 0
Try
Code:
Sub DelRows()
On Error Resume Next
Range("B:B").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub
This will delete any row that has an error
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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