Delete rows on values from 2 columns - code sample included

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I am trying to create a macro that deletes a row based on values in two columns.

Because my column positions are dynamic, they must be found by column (header) name (Customer Number, Total, Order Size)

I thought the code below would do it, but I am getting the error..."Compile Error - End if without block if"

I'm sure it's one silly thing that I just can't figure out and any help would be much appreciated! :)

VBA Code:
[B]Delete any row where the "Order Size" column is small and the "Total" column is less than $500000[/B]
Dim customerCol As Integer
Dim totalCol As Integer
Dim bigorderCol As Integer
 
customerCol = Application.WorksheetFunction.Match("Customer Number", Rows(1).EntireRow, 0)
totalCol = Application.WorksheetFunction.Match("Total", Rows(1).EntireRow, 0)
bigorderCol = Application.WorksheetFunction.Match("Order Size", Rows(1).EntireRow, 0)
 
'Select the last row of customer numbers
Cells(65536, customerCol).End(xlUp).Select
 
'check and loop until back at the top of the sheet
Do While ActiveCell.Row > 1
    If Cells(ActiveCell.Row, totalCol).Value < 500000 _
    And Cells(ActiveCell.Row, bigorderCol).Value = "Small" _
    Then Cells.EntireRow.Delete
    End If
 
    ActiveCell.Offset(-1, 0).Select
 
Loop
 
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Just delete the End If, you have a single line If so it should not have an End If
 
Upvote 0
You have you whole IF/THEN written as one single line (since you are using line continuation characters).
If you have the IF and THEN on the same line, then you do NOT use and END IF statement, i.e.
VBA Code:
    If Cells(ActiveCell.Row, totalCol).Value < 500000 _
    And Cells(ActiveCell.Row, bigorderCol).Value = "Small" _
    Then Cells.EntireRow.Delete

If you want to use the END IF, then remove the underscore after "Small", i.e.
VBA Code:
    If Cells(ActiveCell.Row, totalCol).Value < 500000 _
    And Cells(ActiveCell.Row, bigorderCol).Value = "Small" Then
    Cells.EntireRow.Delete
    End If

By the way, you usually do not need to select cells in VBA in order to work with them. Doing so actually slows down your code.
I would actually replace this whole section of code:

VBA Code:
'Select the last row of customer numbers
Cells(65536, customerCol).End(xlUp).Select

'check and loop until back at the top of the sheet
Do While ActiveCell.Row > 1
    If Cells(ActiveCell.Row, totalCol).Value < 500000 _
    And Cells(ActiveCell.Row, bigorderCol).Value = "Small" _
    Then Cells.EntireRow.Delete
    End If
 
    ActiveCell.Offset(-1, 0).Select
 
Loop
with this:
VBA Code:
    Dim lr As Long
    Dim r As Long
 
'   Find last row with customer numbers
    lr = Cells(Rows.count, customerCol).End(xlUp).Row
 
    Application.ScreenUpdating = False
 
'   Loop through all rows
    For r = lr To 2 Step -1
        If Cells(r, totalCol).Value < 500000 _
            And Cells(r, bigorderCol).Value = "Small" Then
            Rows(r).Delete
        End If
    Next r
 
    Application.ScreenUpdating = True
 
Last edited:
Upvote 0
What can I say?
You guys are truly a wealth of knowledge!
This completely solved my problem!

Thanks so much!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,895
Members
449,194
Latest member
JayEggleton

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