VBA errors while trying to delete rows based on column value

boatbabe

New Member
Joined
Feb 4, 2010
Messages
30
Hi, I am trying to delete rows that have a zero value in column E. I have tried various methods and keep getting 'object does not support this property or method' or 'type mismatch errors. Currently I have:

Code:
For deleterow = .Range("E" & Rows.Count).End(xlUp).Row To 1 Step -1
[B]    If .Range("E" & deleterow).Value = 0 Then[/B]
        Rows(deleterow).EntireRow.Delete
    End If
    Next deleterow

I have also tried
Code:
'For i = lastRow To 1 Step -1
      '  [B]If .Range("E" & i).Value = 0 Then[/B]
     '       Range("E" & i).EntireRow.Delete
     '   End If
   ' Next i
and
Code:
' For i = lastRow To 1 Step -1
    '   [B] If .Range("E" & i).Value2 = 0 Then .Rows(i).Delete[/B]
    
    'Next i
The errors are occurring on the bold lines. Can anyone point me in the direction of what I am doing wrong?

Thanks in advance

Thanks
 
Firstly you have not changed the lines I highlighted.
Secondly if you are getting a Type Mismatch Error, I suspect that the cell value when you get the error is something like #N/A, #VALUE etc.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I believe you are receiving an error on the last cell in the range #Ref !

One way to correct is the change the if formula to value.

This should work.

The others on this post are the real experts, they may have a more efficient solution.

Code:
Range("e1:e" & lastrow).FormulaR1C1 = "=IF(RC[-4]=R[1]C[-4],0,1)"
Range("e1:e" & lastrow).Value = Range("e1:e" & lastrow).Value
 
Upvote 0
Why don't you just use the code in post #2.....it works !!!
 
Upvote 0
Micheal

This is not my post, however I'm interested in learning..

I could be wrong because I'm seldom right... It seems if Column E are formula and not constants Replace Method does not satisfy the OP request.

With Respect...
 
Upvote 0
@billandrew
you're correct, but it's a simple fix !!

Code:
Sub DeleteRows()
    On Error Resume Next
With Columns("E")
    [color=red].Value = .Value[/color]
    .Replace 0, "#N/A", xlWhole, , False
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
End With
End Sub
 
Last edited:
Upvote 0
Hi all, thanks for all your replies. I did reply last night but my post has disappeared so maybe I never pressed send. @michael you are right, it was because column E contained formulae that compared the row to the one below, however the macro was then deleting rows starting from the bottom, resulting in an #N/A error and causing the macro to crash. All sorted now and works perfectly, thanks again, I love this forum :-)
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,048
Members
452,542
Latest member
Bricklin

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