Delete rows based on criteria

MOB

Well-known Member
Joined
Oct 18, 2005
Messages
1,055
Office Version
  1. 365
Platform
  1. Windows
Column A contains entries such as DE0001, DE0002, DE0003 and so on, can be duplicates.

Column K can be empty or contain any entry, text or numeric.

I need a bit of code that;

- rows 6 to "lastrow", if column K is empty, delete the row, unless the value in column A is unique - the row should remain in those instances
- if the value in column A is not unique and column K is empty, delete any of the excess rows, leaving one unique instance remaining

Is there a simple bit of VBA that would achieve this?

TIA
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this. The code works its way up from the bottom to make it a little simpler. If you want it to work top down, then another variable will have to be used to keep track of the row numbers.

Code:
Sub deleteEmptyK()
    Dim i As Long, l as Long
    
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 6 Step -1
        If Cells(i, 11).Value = "" Then
            l = Application.WorksheetFunction.CountIf(Columns(1), Cells(i, 1).Value)
            If l > 1 Then
                Rows(i).Delete
            End If
        End If
    Next i
End Sub
 
Upvote 0
Thanks for the response - I get a run time 1004 error, "Delete method of range class failed" and this line of code is highlighted;

Rows(i).Delete

I'm not too strong with VBA when it doesn't work!

Thanks
 
Last edited:
Upvote 0
About how many rows of data do you have altogether?
 
Upvote 0
At the moment it is a fairly simple monthly spreadsheet that users have to manually delete rows from based on my criteria above in order to create the next month's spreadsheet, and of course I want to automate it as there are 60 of these spreadsheets.....................
 
Last edited:
Upvote 0
Thanks for the response - I get a run time 1004 error, "Delete method of range class failed" and this line of code is highlighted;

Rows(i).Delete

I'm not too strong with VBA when it doesn't work!

Thanks

Actually ignore me, the sheet was protected!! I'll have a play and report back.
 
Upvote 0
Seems to work perfectly, many thanks!!
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,605
Members
449,038
Latest member
Arbind kumar

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