Help with multiple criteria

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I am learning code so please look at the code below. It looks for "Front Link Rod" then deletes the row. What if I want to look for other things as well like "Rear Link Rod", do I put a comma like in red, Well I know thats not right because it doesnt work! How do I write it please?

Rich (BB code):
Sub test()
Dim a As Integer
Dim b As Integer
Range("S1").Select
a = ActiveCell.CurrentRegion.Rows.Count
For b = 1 To a
If Selection.Value = "Front Link Rod, Rear Link Rod" Then
Selection.EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Next b
End Sub
 
This is more compact and more efficient. With very few exceptions it is not necessary to select and selecting slows the code. You have to loop backwards when deleting

Code:
Sub test2()
Dim LR As Long, i As Long
LR = Range("S" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("S" & i).Value = "Front Link Rod" Or Range("S" & i).Value = "Rear Link Rod" Then Rows(i).Delete
Next i
End Sub
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
dazwm,

The only line of code in your original post that should be changed is this line:

Rich (BB code):
  If Selection.Value = "Front Link Rod, Rear Link Rod" Then


Change to this:

Rich (BB code):
  If Selection.Value = "Front Link Rod" Or Selection.Value = "Rear Link Rod" Then
 
Upvote 0
dazwm,

The only line of code in your original post that should be changed is this line:

Rich (BB code):
  If Selection.Value = "Front Link Rod, Rear Link Rod" Then
Change to this:

Rich (BB code):
  If Selection.Value = "Front Link Rod" Or Selection.Value = "Rear Link Rod" Then

Sorry, no. You have to go backwards.
 
Upvote 0
This is more compact and more efficient. With very few exceptions it is not necessary to select and selecting slows the code. You have to loop backwards when deleting

Code:
Sub test2()
Dim LR As Long, i As Long
LR = Range("S" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If Range("S" & i).Value = "Front Link Rod" Or Range("S" & i).Value = "Rear Link Rod" Then Rows(i).Delete
Next i
End Sub

So to clarify your 'LR' is the same as my 'a', and your 'i' is the same as my 'b'. And because the range will start with the first row the 'step -1' will take it to the bottom?
 
Upvote 0
My LR is the last filled row.

For i = LR To 1 Step -1

Loops backwards from the last filled row to row 1 in steps of 1 row.
 
Upvote 0
So

LR = Range("S" & Rows.Count).End(xlUp).Row

Counts the rows from the bottom upwards and my code

a = ActiveCell.CurrentRegion.Rows.Count

Counts from the top downwards?
 
Upvote 0
Yes, correct. The big difference is that yours relies on a cell in column S being selected whereas mine does not.
 
Upvote 0
Yes, correct. The big difference is that yours relies on a cell in column S being selected whereas mine does not.

Sorry if I sound dim but I dont understand
 
Upvote 0
Here you have to select a cell to start with

Code:
Range("S1").Select
a = ActiveCell.CurrentRegion.Rows.Count

Selecting is unnecessary and inefficient.

Code:
LR = Range("S" & Rows.Count).End(xlUp).Row

requires only that the sheet that you wish to operate on is selected.
 
Upvote 0
So should I use your method always or just this particular type of code?
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,391
Members
452,909
Latest member
VickiS

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