Delete & sort macro

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I need to run a delete on a sheet based on criteria. Column B contains UPC's with approximately 1500 rows of Data
that look like what you see below. I want to delete every row EXCEPT the rows that have UPC's that END in 003 and then delete all empty rows.
Any help would be greatly appreciated!

Thank you!!

[TABLE="width: 134"]
<tbody>[TR]
[TD]028200009524/000[/TD]
[/TR]
[TR]
[TD]028200009524/003[/TD]
[/TR]
[TR]
[TD]028200009524/003[/TD]
[/TR]
[TR]
[TD]028200009524/003[/TD]
[/TR]
[TR]
[TD]028200009531/000[/TD]
[/TR]
[TR]
[TD]028200009531/000[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Re: Need help with a delete & sort macro

Code:
Option Explicit


Sub delX()
    Dim lr As Long, i As Long
    lr = Range("B" & Rows.Count).End(xlUp).Row
    For i = lr To 1 Step -1
        If Not InStr(Range("B" & i), "/003") > 0 Then
            Range("B" & i).EntireRow.Delete
        End If
    Next i


End Sub
 
Upvote 0
Re: Need help with a delete & sort macro

Another option
Code:
Sub Buns1976()
   With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(right(@,3)=""003"",@,"""")", "@", .Address))
      .SpecialCells(xlBlanks).EntireRow.Delete
   End With
End Sub
 
Upvote 0
Re: Need help with a delete & sort macro

Thanks Fluff,

If I could trouble you for one more please?

Same scenario where UPC's are in Column B except we want to delete all rows except those that Begin with 02820000.

[TABLE="width: 143"]
<tbody>[TR]
[TD="class: xl63, width: 143"]028200003577/003[/TD]
[/TR]
</tbody>[/TABLE]


Thank you!
 
Upvote 0
Re: Need help with a delete & sort macro

Fluff,

Never mind I got it.

Thanks so much for your help!
 
Upvote 0
Re: Need help with a delete & sort macro

Glad you figured it out & thanks for the feedback
 
Upvote 0
Re: Need help with a delete & sort macro

Hi Fluff,

Ran into an obstacle with the macro. We are importing the data from another workbook and it is coming in as a table.
When I run the macro it deletes any cells in column B if they DO NOT end in 003 but fails to delete those rows.
Don't understand that as it works fine in a normal range of cells? Can you take a look at it please?

Thanks!

Code:
Sub COMPACT_PLU_IMPORT()    With Range("B2", Range("B" & Rows.Count).End(xlUp))
      .Value = Evaluate(Replace("if(right(@,3)=""003"",@,"""")", "@", .Address))
      .SpecialCells(xlBlanks).EntireRow.Delete
   End With
End Sub
 
Upvote 0
Re: Need help with a delete & sort macro

Easiest option to to convert the table to a range prior to running the macro.
 
Upvote 0
Re: Need help with a delete & sort macro

Hi Fluff,

Unfortunately when I convert to range I lose my connection to the import sheet. Not sure what the best way to go about this would be?

Thanks!
 
Upvote 0
Re: Need help with a delete & sort macro

I've never used linked/imported tables, so not sure what to suggest.
Especially as I don't know what impact deleting rows may have on your data.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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