VBA Loop Help

ExcelKid_10

Board Regular
Joined
Mar 17, 2004
Messages
87
Hi All-

Can somebody help me with this code. I cannot get it to loop through for each criteria (Str1, Str2) and delete the rows. I have to run the code 7 times to get them all cleared. Any thoughts are much appreciated as always! Thanks! EK

Sub CheckValues2()
Dim rwIndex As Integer
Dim colIndex As Integer
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Str4 As String
Dim Str5 As String
Dim Str6 As String
Dim Str7 As String

Str1 = "Average/Total "
Str2 = "Median "
Str3 = "Max "
Str4 = "25th Percentile "
Str5 = "50th Percentile "
Str6 = "75th Percentile "
Str7 = "Min "

For rwIndex = 1 To 1500
For colIndex = 2 To 2
If Cells(rwIndex, colIndex).Value = Str1 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str2 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str3 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str4 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str5 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str6 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str7 Then
Cells(rwIndex, colIndex).EntireRow.Delete

End If

Next colIndex
Next rwIndex
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

iggydarsa

Well-known Member
Joined
Jun 28, 2005
Messages
1,734
Office Version
  1. 2019
Platform
  1. Windows
instead of this
Code:
For rwIndex = 1 To 1500
For colIndex = 2 To 2
If Cells(rwIndex, colIndex).Value = Str1 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str2 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str3 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str4 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str5 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str6 Then
Cells(rwIndex, colIndex).EntireRow.Delete
ElseIf Cells(rwIndex, colIndex).Value = Str7 Then
Cells(rwIndex, colIndex).EntireRow.Delete

End If

Next colIndex
Next rwIndex

maybe you should try this

Code:
For rwIndex = 1500 To 1 Step -1
If Cells(rwIndex, 2).Value = Str1 or Cells(rwIndex, 2).Value = Str2 or Cells(rwIndex, 2).Value = Str3 or Cells(rwIndex, 2).Value = Str4 or Cells(rwIndex, 2).Value = Str5 or Cells(rwIndex, 2).Value = Str6 or Cells(rwIndex, 2).Value = Str7 Then
    Cells(rwIndex, 2).EntireRow.Delete
End If
Next rwIndex
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
You could use this:
Code:
Sub CheckValues2()
Dim rwIndex As Integer
Dim colIndex As Integer
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Str4 As String
Dim Str5 As String
Dim Str6 As String
Dim Str7 As String

Str1 = "Average/Total "
Str2 = "Median "
Str3 = "Max "
Str4 = "25th Percentile "
Str5 = "50th Percentile "
Str6 = "75th Percentile "
Str7 = "Min "

For rwIndex = 1 To 1500
    For colIndex = 2 To 2 ' why is there a loop here?
        Select Case Cells(rwIndex, colIndex).Value
            Case Str1, Str2, Str3, Str4, Str5, Str6, Str7
                Cells(rwIndex, colIndex).EntireRow.Delete
            Case Else
                ' do nothing
        End Select
    Next colIndex
Next rwIndex
End Sub
if you prefer.
 
Upvote 0

Forum statistics

Threads
1,190,790
Messages
5,982,928
Members
439,807
Latest member
WXM86

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
Top