Filter & Delete Deleting Header Row Suddenly

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I filter and delete records fairly frequently and haven't noticed this issue before. I typically use
VBA Code:
'If sDLR > 1 Then sD.Range("A2:A" & sDLR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
but for some reason, it's deleting the header row. I tried a few other things that I found on the web and they all deleted the header row as well; except for this one
VBA Code:
sD.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
. Could there be something about this particular data file that is causing the snippet I usually use to fail, or do I need to go and change all of my filter & delete related code?

VBA Code:
Dim m As Workbook, s As Workbook
Dim mD As Worksheet, sD As Worksheet, mV As Worksheet
Dim fP As String, fN As String, fE As String, uDP As String
Dim r As Range
Dim mDLR As Long, mNLR As Long, sDLR As Long, mVLR As Long

Set m = ThisWorkbook
Set mD = m.Sheets("New Data")
Set mV = m.Sheets("Variables")

mDLR = mD.Range("A" & Rows.Count).End(xlUp).Row
mVLR = mV.Range("L" & Rows.Count).End(xlUp).Row

uDP = CreateObject("WScript.Shell").SpecialFolders("Desktop")

fP = uDP & "\Import Files\"
fN = "SE"
fN = Dir(fP & fN & "*.xlsx")

On Error Resume Next
Set s = Workbooks.Open(fP & fN)
Set sD = s.Sheets("Data")
On Error GoTo 0

If s Is Nothing Then GoTo MissingFile
If sD Is Nothing Then GoTo MissingSheet

'Removes filters from the working data if any exist.
If sD.AutoFilterMode Then sD.AutoFilterMode = False

'Unhides any columns and rows that may be hidden on the working data.
With sD.UsedRange
    .Columns.EntireColumn.Hidden = False
    .Rows.EntireRow.Hidden = False
End With

sDLR = sD.Range("A" & Rows.Count).End(xlUp).Row

'Delete column A.
sD.Columns("A").Delete

sD.Range("P1").Value = "Action"

With sD.Range("P2:P" & sDLR)
    .Value = "=IF(OR(RC[-3]="""",RC[-3]=RC[-11]),""Delete"",""Keep"")"
End With

'Filter out blank 2nd  Names
sD.UsedRange.AutoFilter Field:=16, Criteria1:=Array("Delete"), Operator:=xlFilterValues
'r.AutoFilter Field:=16, Criteria1:="Delete", Operator:=xlFilterValues

sDLR = sD.Range("A" & Rows.Count).End(xlUp).Row

'Delete blank 2nd Level Exception Names and 1st & 2nd Name matches.
'If sDLR > 1 Then sD.Range("A2:A" & sDLR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
'If sDLR > 1 Then r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
'If sDLR > 1 Then
'    Set r = sD.Range("A2:A" & sDLR).SpecialCells(xlCellTypeVisible)
'    r.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
'End If
sD.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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