Deleting rows that are not on a second list

kadain

New Member
Joined
May 19, 2022
Messages
22
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi Guys and Gals
Bit Stumped on wraping my head round how to get his done but basicly am looking to make a macro that would look threw a list of unique code then delete any codes that are not on the list in a complete list
The complete list of products looks a little like this(information changed for Legal Reasons), looking for the user to put in the unque codes in to a second sheet and remove the ones not on the list in the list of products

Sample Wine June22 v1.1.xlsx
ABCDEFGHIJ
1
2
3Wine Product Price list effective 1st June 2022 (v1.1)
4Product CodeProduct DescriptionVeganVegetarianOrganicProduct SizeQty per Case
5
6Argentina
7Argentina RedRegion
8324234234Ben Marco Pinot Noir 2017MendozaVVBottle6
922244565Benegas Ataliva Malbec 2021MendozaVVBottle 
102131311Benegas Estate Cabernet Franc 2016MendozaVVBottle 
1113131231Benegas Estate Malbec 2020MendozaVVBottle 
1222423234Crios Tannat Mendoza Argentina 2018MendozaVVBottle6
1335345345Finca la Nina Malbec 2021MendozaBottle12
14323113Goyenechea 5th Generacion Malbec Gran Reserva 2014San RafaelBottle12
1542414124Goyenechea Cabernet Sauvignon 2020San RafaelBottle12
1632422423Goyenechea Centenario Malbec Reserva 2019San RafaelBottle12
17234234322Goyenechea Lorenza Bonarda 2017San RafaelBottle12
1823423423Goyenechea Malbec Mendoza 2020San RafaelBottle12
1912411424Goyenechea Syrah 2016San RafaelBottle12
2034534534Hanger And Flank MalbecMendoza24 X Qtr1
2114414124Hanger And Flank Malbec 2020MendozaBottle12
2243534345Juan Benegas Malbec 2020MendozaVVBottle 
233342424Luna Benegas Cabernet Sauvignon 2020MendozaVVBottle 
24534535353Montanes Malbec Argentina 2020MendozaBottle12
25345353434Susana Balbo Crios Cabernet SauvignonMendozaVVBottle6
26345345345Susana Balbo Late Harvest Malbec 2019MendozaVV50 Cl12
27242423454Terrazas de Los Andes Select MalbecMendozaBottle6
28
29Argentina RoséRegion
301231313131Crios Rosé Of Malbec 2019MendozaVVBottle6
313131325Carmela Benegas Rosé 2021MendozaVVBottle 
323233131231Goyenechea Merlot Rosé Mendoza 2020San RafaelBottle12
33
34Argentina WhiteRegion
Wine June22 v1.1
Cell Formulas
RangeFormula
H9:H11,H31,H22:H23H9=IFERROR(VLOOKUP(A9,'https://inveraritymorton1.sharepoint.com/sites/SalesOps/Shared Documents/Sales Ops Private/Pricing and PINC/PINC/PINC June ''22/Team folder/[Wines June22 v1.1 (Andrew-Aidan) Working Doc.xlsx]Status'!A:G,5,FALSE),"")


i need it not to delete the spaces between the catagory headers and the catagory headers too aswell
bit of a head scratcher for me but am sure the comminty could help out here
i look forward to see what yous come up with
P.S. there is another two colounms i had to remove for legal Reasons thats why there two blanks at the end
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I think something like this might work for you. I commented the lines that you will need to adjust to the specifics of your workbook:
VBA Code:
Sub ProductRemoval()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long
Dim j As Long
Dim lrow1 As Long
Dim lrow2 As Long
Dim ProductIDClm As Integer
Dim Check As Boolean

Set ws1 = Sheets("Complete List") 'Change this to the name of the sheet with the full list
Set ws2 = Sheets("Keep List") 'change this to the name of the sheet with the products you'd like to keep

ProductIDClm = 1 'This represents that column you're product IDs are in. I have currently set to 1, which = column A. (2 = column B, 3 = column C, etc...). This macro assumes your IDs will be in the same column in both sheets (Column A)

lrow1 = ws1.Cells(Rows.Count, ProductIDClm).End(xlUp).Row
lrow2 = ws1.Cells(Rows.Count, ProductIDClm).End(xlUp).Row

For i = lrow1 To 1 Step -1
    If Application.IsNumber(ws1.Cells(i, ProductIDClm)) = True Then
        Check = False
        
        For j = 1 To lrow2
            If ws1.Cells(i, ProductIDClm) = ws2.Cells(j, ProductIDClm) Then Check = True
        Next j
        
        If Check = False Then Cells(i, ProductIDClm).EntireRow.Delete
    End If

Next i

End Sub
 
Upvote 0
Solution
I think something like this might work for you. I commented the lines that you will need to adjust to the specifics of your workbook:
VBA Code:
Sub ProductRemoval()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long
Dim j As Long
Dim lrow1 As Long
Dim lrow2 As Long
Dim ProductIDClm As Integer
Dim Check As Boolean

Set ws1 = Sheets("Complete List") 'Change this to the name of the sheet with the full list
Set ws2 = Sheets("Keep List") 'change this to the name of the sheet with the products you'd like to keep

ProductIDClm = 1 'This represents that column you're product IDs are in. I have currently set to 1, which = column A. (2 = column B, 3 = column C, etc...). This macro assumes your IDs will be in the same column in both sheets (Column A)

lrow1 = ws1.Cells(Rows.Count, ProductIDClm).End(xlUp).Row
lrow2 = ws1.Cells(Rows.Count, ProductIDClm).End(xlUp).Row

For i = lrow1 To 1 Step -1
    If Application.IsNumber(ws1.Cells(i, ProductIDClm)) = True Then
        Check = False
       
        For j = 1 To lrow2
            If ws1.Cells(i, ProductIDClm) = ws2.Cells(j, ProductIDClm) Then Check = True
        Next j
       
        If Check = False Then Cells(i, ProductIDClm).EntireRow.Delete
    End If

Next i

End Sub

thank you for the your help, i tried this macro in my workbook, renamed the two sheets and it didnt seem to work, all the other products still remain
 
Upvote 0
I think something like this might work for you. I commented the lines that you will need to adjust to the specifics of your workbook:
VBA Code:
Sub ProductRemoval()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long
Dim j As Long
Dim lrow1 As Long
Dim lrow2 As Long
Dim ProductIDClm As Integer
Dim Check As Boolean

Set ws1 = Sheets("Complete List") 'Change this to the name of the sheet with the full list
Set ws2 = Sheets("Keep List") 'change this to the name of the sheet with the products you'd like to keep

ProductIDClm = 1 'This represents that column you're product IDs are in. I have currently set to 1, which = column A. (2 = column B, 3 = column C, etc...). This macro assumes your IDs will be in the same column in both sheets (Column A)

lrow1 = ws1.Cells(Rows.Count, ProductIDClm).End(xlUp).Row
lrow2 = ws1.Cells(Rows.Count, ProductIDClm).End(xlUp).Row

For i = lrow1 To 1 Step -1
    If Application.IsNumber(ws1.Cells(i, ProductIDClm)) = True Then
        Check = False
       
        For j = 1 To lrow2
            If ws1.Cells(i, ProductIDClm) = ws2.Cells(j, ProductIDClm) Then Check = True
        Next j
       
        If Check = False Then Cells(i, ProductIDClm).EntireRow.Delete
    End If

Next i

End Sub
also sorry for the long waited reply, my patner took not well over the long weekend
 
Upvote 0
Maybe even change it up and keep the rows that have a "yes" or "y" in an extra coloumn at the end and delete the rest
 
Upvote 0
Do you know if your product IDs are formatted as Numbers?

To check this, you can use the formula =Type(A1) on your product IDs to see if it = 1 (1 = Number).

the macro I wrote requires the values to be a number and will ignore everything that's not a number.
 
Upvote 0
Do you know if your product IDs are formatted as Numbers?

To check this, you can use the formula =Type(A1) on your product IDs to see if it = 1 (1 = Number).

the macro I wrote requires the values to be a number and will ignore everything that's not a number.
The product id’s are a few numbers followed by a letter unfortunately I can’t change these
 
Upvote 0
So ive been working away at some of my own coding and and managed to get it to the point where its deleted everything except the products i want to keep and all the region headers, alot of them dont have any information in them due to them being removed prducts, so now working on a way to delete off the unwated one, was thinking mayone something that identifys that colounm A + B have values keep the first bold it find above it

here an example of what am left with after i run my code and how the product id looks
Wine v1.1 macro.xlsm
ABCDEFGH
15Australia
16Australia RedRegion
1732145YNormans Holbrooks Rd Shiraz 2019South AustraliaBottle12
18
19Australia SparklingRegion
20
21Australia WhiteRegion
22
23Austria RedRegion
24
25Austria WhiteRegion
26
27Chile
28Chile RedRegion
29
30Chile RoséRegion
31
32Chile WhiteRegion
3354321RSenora Rosa Sauvignon Blanc 2021Central ValleyBottle12
34
35England
36England SparklingRegion
37
38France
39France Sparkling WineRegion
40
41France Branded ChampagneRegion
4212345TAyala Brut Majeur N.V.AyBottle6
43
Complete List Helper


i dont know if this would be any easier than redoing the orignal code you gave me but thought it might help
hoping to hear from you soon
 
Upvote 0
Try this for removing the empty records:
VBA Code:
Sub Remove()

For i = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1

    If Cells(i, 1) = "" And Cells(i, 2) = "" Then
        Cells(i, 2).EntireRow.Delete
        GoTo Nexti
    End If

    If Cells(i, 1) = "" And Cells(i, 2) <> "" And Cells(i, 2).Font.Bold = True And (Cells(i + 1, 1).Font.Bold = True Or Cells(i + 1, 1) = "") Then
        Cells(i, 2).EntireRow.Delete
        GoTo Nexti
    End If
    
    If Cells(i, 1) <> "" And Cells(i, 1).Font.Bold = True And Cells(i + 2, 1) = "" Then
        Cells(i, 2).EntireRow.Delete
        GoTo Nexti
    End If
    
Nexti:
Next i

End Sub
 
Upvote 0
Try this for removing the empty records:
VBA Code:
Sub Remove()

For i = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1

    If Cells(i, 1) = "" And Cells(i, 2) = "" Then
        Cells(i, 2).EntireRow.Delete
        GoTo Nexti
    End If

    If Cells(i, 1) = "" And Cells(i, 2) <> "" And Cells(i, 2).Font.Bold = True And (Cells(i + 1, 1).Font.Bold = True Or Cells(i + 1, 1) = "") Then
        Cells(i, 2).EntireRow.Delete
        GoTo Nexti
    End If
   
    If Cells(i, 1) <> "" And Cells(i, 1).Font.Bold = True And Cells(i + 2, 1) = "" Then
        Cells(i, 2).EntireRow.Delete
        GoTo Nexti
    End If
   
Nexti:
Next i

End Sub
Absoultly Spot on, thank you so much, if you have the brake down of what each line does so i can learn it a bit better that would be amazing
10/10 recommend again
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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