Delete row if a specific value exist (VBA)

eitan932

New Member
Joined
Jan 31, 2017
Messages
15
hello

i'm using code based on Ron de Bruin's site
I want to delete row/s if a specific value exist on either 2 columns
Basically, I want to delete value 1 or 2 on column "A"
and "955.46*" on column "D"


i'm getting run time error '424' what I'm doing wrong??
(line "If Not IsError(.Value) Then" highlighted)

Code:
Sub delete0()
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'We use the ActiveSheet but you can replace this with
'Sheets("MySheet")if you want
With ActiveSheet
'We select the sheet so we can change the window view
.Select
'If you are in Page Break Preview Or Page Layout view go
'back to normal view, we do this for speed
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
'Turn off Page Breaks, we do this for speed
.DisplayPageBreaks = False
'Set the first and last row to loop through
Firstrow = .UsedRange.Cells(1).Row
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
'We loop from Lastrow to Firstrow (bottom to top)
For Lrow = Lastrow To Firstrow Step -1
'We check the values in the A column in this example
With .Cells(Lrow, "A") And .Cells(Lrow, "D")
If Not IsError(.Value) Then
If .Value = "1" Or "2" Or Value Like "955.46*" Then .EntireRow.Delete
End If
End With
Next Lrow
End With
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
End Sub

Thanks
Eitan
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">[TABLE="width: 800"]
<tbody>[TR]
[TD][h=1]Ron de Bruin[/h][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 800"]
<tbody>[TR]
[TD][h=1]Ron de Bruin[/h][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 800"]
<tbody>[TR]
[TD][h=1]Ron de Bruin[/h][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 800"]
<tbody>[TR]
[TD][h=1]Ron de Bruin[/h][/TD]
[/TR]
</tbody>[/TABLE]
</body>
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi,

Not sure about why your code is failing, but you might find the code below quicker and easier to maintain. I often use this method.

Code:
Sub ClearOut()
    
    Range("A1").Select
    
    ActiveSheet.UsedRange.AutoFilter _
        Field:=4, Criteria1:=">=955.46", Operator:=xlAnd, Criteria2:="<955.47"
    
    ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="1"
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.UsedRange.AutoFilter Field:=1
    
    ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="2"
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.UsedRange.AutoFilter Field:=1
        
    ActiveSheet.UsedRange.AutoFilter Field:=4


End Sub
 
Upvote 0
Thank you

I tried your method but it didn't work for me.
Data I need to delete looks like this:
955.460.001.1
955.460.541.1
955.461.654.1

I want to delete all rows begin with: "955.46"
Titles row is row no 4
and I need to use that filter on column D, and delete all 955.46... on row 5 to last row of data.

Thanks in advance
 
Upvote 0
Hi,

I assumed this was a money value.

Try line below instead. Change Rows("2:2") to Rows("5:5").

ActiveSheet.UsedRange.AutoFilter _
Field:=4, Criteria1:="=955.46*", Operator:=xlAnd
 
Upvote 0
Please excuse me if you already know or do this ...

I write most of my code by recording it.

Click the Developer tab in excel then click Record Macro.

Manually do what you want the macro to do.

Click Stop Recording and then make any required adjustments to your VBA code.
 
Upvote 0
Please excuse me if you already know or do this ...

I write most of my code by recording it.

Yes I know.
I manage to do it.

Here what I've done:
Code:
Sub test()Dim LR As Integer
LR = Range("D" & Rows.Count).End(xlUp).Row
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("D4:J" & LR).Select
With Selection
.AutoFilter Field:=1, Criteria1:="=*955.46*"
End With


    Range("D5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.EntireRow.Delete
    
End Sub
 
Upvote 0
more accurate

Code:
Sub test()Dim LR As Integer
LR = Range("D" & Rows.Count).End(xlUp).Row
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("A4:J" & LR).Select
    
ActiveSheet.AutoFilterMode = False
ActiveSheet.Range("D4:J" & LR).Select
With Selection
.AutoFilter Field:=1, Criteria1:="=*955.46*"
End With


    Range("D5").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.EntireRow.Delete


ActiveSheet.Range("A4:J" & LR).AutoFilter Field:=1


Range("A5").Select
    
End Sub
 
Upvote 0
I thought my problem solved, but there is still a problem, if i will run this macro twice or more - it delete more rows, even if they are not on the category (955.46....)
Why is that??
The filter works perfectly, but the delete mechanism not work as expected
 
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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