I filter and delete records fairly frequently and haven't noticed this issue before. I typically use
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
. 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:
'If sDLR > 1 Then sD.Range("A2:A" & sDLR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
VBA Code:
sD.AutoFilter.Range.Offset(1, 0).EntireRow.Delete
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