Delete row if NOT equal to MULTIPLE Values

adamvba

New Member
Joined
Oct 30, 2006
Messages
4
Hi!

I am having problems deleting multiple rows .... below is the macro to delete rows that is equal to a specific value. I need one that can delete rows if not equal to specific values. I would really appreciate help on this!

Thanks

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

ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView

Firstrow = ActiveSheet.UsedRange.Cells(1).Row
Lastrow = ActiveSheet.UsedRange.Rows.Count + Firstrow - 1

With ActiveSheet
.DisplayPageBreaks = False
For Lrow = Lastrow To Firstrow Step -1

If IsError(.Cells(Lrow, "R").Value) Then
'Do nothing, This avoid a error if there is a error in the cell

ElseIf .Cells(Lrow, "R").Value = "0" Then .Rows(Lrow).Delete
'This will delete each row with the Value "0" in Column R, case sensitive.

End If
Next
End With

ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.Calculation = CalcMode
End With
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
hi adavba

try this code it will delete all rows except where column r = either "0","2" and "4"
Code:
Option Explicit

Sub test()
Dim a()
Dim R, j, I, r1
Set R = Columns("r:r")
R = R.Value2

For j = 1 To Range("r65536").End(xlUp).Row
    Select Case R(j, 1)
    Case "2", "0", "4"
    
    Case Else
        I = I + 1
        ReDim Preserve a(I)
        a(I) = j
    End Select
Next

Set r1 = Rows(a(1))
For j = 2 To UBound(a)
     Set r1 = Union(r1, Rows(a(j)))
Next
r1.Delete
End Sub
 
Upvote 0
Post PM
Hi bbrnx19

Hope all is well...thanks for your help. It now gets passed Set R = Worksheets("test").Columns("r:r") but get stuck on the below:

If R(j, 1) = "US" Or R(j, 1) = "UN" Or R(j, 1) = "UQ" Or R(j, 1) = "UR"

Do you have any ideas?? As always I'm most appreciative of your help.

Best,

adamvba

Sub Deleterows()


Dim a()
Dim R, j, I, r1

Set R = Worksheets("test").Columns("r:r")
R = R.Value2

For j = 1 To Range("A65536").End(xlUp).Row
If R(j, 1) = "US" Or R(j, 1) = "UN" Or R(j, 1) = "UQ" Or R(j, 1) = "UR" Then
Else
I = I + 1
ReDim Preserve a(I)
a(I) = j
End If
Next

Set r1 = Rows(a(1))
For j = 2 To UBound(a)
Set r1 = Union(r1, Rows(a(j)))
Next
r1.Delete

End Sub

hi adam
I found if statements will take only two Boolean statement “if a=b or a=c then” you can add multiply if statements but can be confusing

so I use the Select Case see below


Code:
Select case R(j, 1) 
case  "US" , "UN" ,"UQ" , "UR" 

case Else
       I = I + 1
        ReDim Preserve a(I)
        a(I) = j
 End Select

will add this to the orginal thread as well
 
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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