I'm running into some trouble deleting some rows using VBA.
I have data organised in rows in Columns A - K. In column K there is a counter; when it exceeds 1 the entire row needs to be deleted. I set up the following code; and expected it to filter on column K for anything exceeding 1. If it finds that there are more than 1 rows (row 1 is the header), it would delete anything visible, else it just removes the criteria from the filter.
However, the first Message Box returns a value of 2,900 (correct) then the second message box returns a value of 1, and I have no idea why. Consequently, none of the rows with column K exceeding 1 (there are about 2,000) get removed.
Visible Rows is defined at the start of the macro as Long.
With MySheet
'Find new lastrow
lRowDbMsNew = .Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).ROW
.Range("A:K").Calculate
MsgBox ("The last row in the data is " & lRowDbMsNew)
.Range("A1:A" & lRowDbMsNew).AutoFilter Field:=11, Criteria1:=">1"
VisibleRows = .Range("A1:A" & lRowDbMsNew).SpecialCells(xlCellTypeVisible).Rows.Count
MsgBox ("Number of visible rows: " & VisibleRows)
If VisibleRows > 1 Then
.Range("A2:A" & lRowDbMsNew).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.Range("A1:K" & lRowDbMsNew).AutoFilter Field:=11
Else
.Range("A1:K" & lRowDbMsNew).AutoFilter Field:=11
End If
End With