Shorten/simplify/improve/make more efficient this bit of code

Truiz

Active Member
Joined
Jul 14, 2014
Messages
339
Good day lads,

I have this code

Code:
For iCntr = lRow To 1 Step -1                                           If Cells(iCntr, "L") = "Close" Then                                 
    Rows(iCntr).Delete                                                  
    End If                                                              
    Next                                                                
                                                                        
    For iCntr = lRow To 1 Step -1                                       
    If Cells(iCntr, "L") = "Close Supervised + Auto Notify" Then        
    Rows(iCntr).Delete                                                  
    End If                                                              
    Next                                                                
                                                                        
    For iCntr = lRow To 1 Step -1                                       
    If Cells(iCntr, "L") = "Fail to Close" Then                         
    Rows(iCntr).Delete                                                  
    End If                                                              
    Next                                                                
                                                                        
    For iCntr = lRow To 1 Step -1                                       
    If Cells(iCntr, "L") = "Fail To Open" Then                          
    Rows(iCntr).Delete                                                  
    End If                                                              
    Next


I wrote it as such cuz i'm no expert in VBA I was wondering if there is a way to shorten the code I mean it works and everything I just think in looks dirty to have that.

Regards,
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Untested but how about
Code:
    Dim Arr() As Variant

    Set Arr = Array("Close", "Close Supervised + Auto Notify", "Fail to Close", "Fail To Open")

    For iCntr = lrow To 1 Step -1
        If UBound(Filter(Arr, Cells(iCntr, "L"), , vbTextCompare)) >= 0 Then
            Rows(iCntr).Delete
        End If
    Next
 
Upvote 0
Here is another way to consider...
Code:
[table="width: 500"]
[tr]
	[td]For iCntr = lRow To 1 Step -1
  Select Case Cells(iCntr, "L")
    Case "Close", "Close Supervised + Auto Notify", "Fail to Close", "Fail To Open"
      Rows(iCntr).Delete
  End Select
Next[/td]
[/tr]
[/table]
Note: You spelled the word "to" differently in "Fail to Close" and "Fail To Open". I used the spelling you specified but would point out that letter casing for the text in your cells is important and must exactly match the text used in the code above. If you cannot be sure the text will always be entered exactly the same way, then use the following version of my code instead...
Code:
[table="width: 500"]
[tr]
	[td]For iCntr = lRow To 1 Step -1
  Select Case LCase(Cells(iCntr, "L"))
    Case "Close", "close supervised + auto notify", "fail to close", "fail to open"
      Rows(iCntr).Delete
  End Select
Next[/td]
[/tr]
[/table]
 
Upvote 0
Another way:
Code:
Sub Truiz()
Dim Unwanted As Variant, lRow As Long, j As Long
Unwanted = Array("Close", "Close Supervised + Auto Notify", "Fail to Close", "Fail To Open")
lRow = Range("L" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
With Range("L1:L" & lRow)
    For j = LBound(Unwanted) To UBound(Unwanted)
        .Replace Unwanted(j), "#N/A", xlWhole
    Next j
End With
On Error Resume Next
Range("L1:L" & lRow).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
A fourth suggestion, using filter instead of loop:
Code:
Sub Remove()

    Dim x           As Long
    Dim strFiler    As Variant
    
    strfilter = Array("Close", "Close Supervised + Auto Notify", "Fail to Close", "Fail To Open")
    
    With ActiveSheet
        x = .Cells(.Rows.count, 12).End(xlUp).row
        With .Cells(1, 12).Resize(x)
            On Error Resume Next
            .AutoFilter Field:=1, Criteria1:=strfilter, Operator:=xlFilterValues
            .Offset(1).Resize(x - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            On Error GoTo 0
        End With
        .AutoFilterMode = False
    End With
    
    Erase strfilter
    
End Sub
As you can see, depending on your preference "Simplify/Improve/Make more efficient" can lead to lots of suggestions!
 
Last edited:
Upvote 0
Here is another way to consider...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]For iCntr = lRow To 1 Step -1
  Select Case Cells(iCntr, "L")
    Case "Close", "Close Supervised + Auto Notify", "Fail to Close", "Fail To Open"
      Rows(iCntr).Delete
  End Select
Next[/TD]
[/TR]
</tbody>[/TABLE]
Note: You spelled the word "to" differently in "Fail to Close" and "Fail To Open". I used the spelling you specified but would point out that letter casing for the text in your cells is important and must exactly match the text used in the code above. If you cannot be sure the text will always be entered exactly the same way, then use the following version of my code instead...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]For iCntr = lRow To 1 Step -1
  Select Case LCase(Cells(iCntr, "L"))
    Case "Close", "close supervised + auto notify", "fail to close", "fail to open"
      Rows(iCntr).Delete
  End Select
Next[/TD]
[/TR]
</tbody>[/TABLE]


I never noticed that I got the list from the original report I guess luckily it worked
 
Upvote 0
A fourth suggestion, using filter instead of loop:
Code:
Sub Remove()

    Dim x           As Long
    Dim strFiler    As Variant
    
    strfilter = Array("Close", "Close Supervised + Auto Notify", "Fail to Close", "Fail To Open")
    
    With ActiveSheet
        x = .Cells(.Rows.count, 12).End(xlUp).row
        With .Cells(1, 12).Resize(x)
            On Error Resume Next
            .AutoFilter Field:=1, Criteria1:=strfilter, Operator:=xlFilterValues
            .Offset(1).Resize(x - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            On Error GoTo 0
        End With
        .AutoFilterMode = False
    End With
    
    Erase strfilter
    
End Sub
As you can see, depending on your preference "Simplify/Improve/Make more efficient" can lead to lots of suggestions!

EXACTLY what I wanted looking at how others tackle the task does lead to lots of suggestion suggestions that at one point will all become useful as I always say Information is the one thing you can't have to much of
 
Upvote 0
I encountered an issue.

One of the strings I want to delete is AR/Billing "Insurance" as you can see it has quotation marks, thus creating a syntax error
 
Upvote 0
Instead, could you delete anything that begins "AR/Billing*"with use of a wild card?
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,446
Latest member
CodeCybear

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