I have a problem trying to delete an entire row using the code as below:
Sub Macro1()
Dim test1row As Long, test1 As Range, firstrowtodelete As Long
test1row = 1
Do
Set test1 = Sheets("Sheet1")l.Cells(test1row,1)
If test1 <> "actest" Or test1 <> "dctest" Then
firstrowtodelete = test1row
Rows(test1row).Select
Selection.Delete Shift:=xlUp
Exit Do
End If
test1row = test1row + 1
Loop
End Sub
The error is with Rows(test1row).Select. It deletes the row even if it is “actest” or “dctest”. If you know why do share with me! thanks! 🙂
There are two problems with your code.
The first problem is on this line:
It should be:
And the other problem is here:
It should be this:
Your
Ifstatement would always be true. Using theAndoperator will give you a true value only when the cell’s value isn’t “actest” and it isn’t “dctest” which is presumably the desired behavior.Here is the complete working code: