Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
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!!

028200009524/000
028200009524/003
028200009524/003
028200009524/003
028200009531/000
028200009531/000

<tbody>
</tbody>
 
Last edited:

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,852
Office Version
  1. 2019
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,303
Office Version
  1. 365
Platform
  1. Windows
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
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
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.

028200003577/003

<tbody>
</tbody>


Thank you!
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138

ADVERTISEMENT

Re: Need help with a delete & sort macro

Fluff,

Never mind I got it.

Thanks so much for your help!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,303
Office Version
  1. 365
Platform
  1. Windows
Re: Need help with a delete & sort macro

Glad you figured it out & thanks for the feedback
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138

ADVERTISEMENT

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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,303
Office Version
  1. 365
Platform
  1. Windows
Re: Need help with a delete & sort macro

Easiest option to to convert the table to a range prior to running the macro.
 

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
138
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!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,303
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,974
Messages
5,525,993
Members
409,673
Latest member
Riseee

This Week's Hot Topics

Top