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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try to use AND

Code:
if Selection.Value = "Front Link Rod" and Selection.Value ="Rear Link Rod" Then
 
Upvote 0
I tried it with OR and that seemed to work.
 
Upvote 0
dazwm,

When you are inserting or deleting rows is always better to loop from the last row to the first row.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub test()
Dim a As Long
Dim b As Long
Range("S1").Select
a = ActiveCell.CurrentRegion.Rows.Count
For b = a To 1 Step -1
  If Selection.Value = "Front Link Rod" Or Selection.Value = "Rear Link Rod" Then
    Selection.EntireRow.Delete
  Else
    Selection.Offset(-1, 0).Select
  End If
Next b
End Sub
 
Upvote 0
dazwm,

When you are inserting or deleting rows is always better to loop from the last row to the first row.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub test()
Dim a As Long
Dim b As Long
Range("S1").Select
a = ActiveCell.CurrentRegion.Rows.Count
For b = a To 1 Step -1
  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

Would you mind explaining what is different in the code please, especially the step -1 part. Thanks
 
Upvote 0
dazwm,

When you are inserting or deleting rows it is always better to loop from the last row to the first row.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Rich (BB code):
Option Explicit
Sub test()
Dim a As Long
Dim b As Long
Range("S1").Select
a = ActiveCell.CurrentRegion.Rows.Count
For b = a To 1 Step -1
  If Selection.Value = "Front Link Rod" Or Selection.Value = "Rear Link Rod" Then
    Selection.EntireRow.Delete
  Else
    Selection.Offset(-1, 0).Select
  End If
Next b
End Sub
 
Upvote 0
You have just copied the previous post. I dont understand how in the code it is starting at the bottom.
 
Upvote 0
dazwm,

Sorry, I am getting ahead of myselff.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Rich (BB code):
Option Explicit
Sub test()
Dim a As Long
Dim b As Long
Range("S1").Select
a = ActiveCell.CurrentRegion.Rows.Count
For b = 1 To a
  If Selection.Value = "Front Link Rod" Or Selection.Value = "Rear Link Rod" Then
    Selection.EntireRow.Delete
  Else
    Selection.Offset(1, 0).Select
  End If
Next b
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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