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
 
I added comments to the code to try and explain each line as best as I could:
VBA Code:
Sub Remove()

'The goal of this is to loop through each row, and evaluate the 3 "If" statements below. If any of those fire as True, then delete the row and move to the next row.
'I'm using i as an integer that represents each row as the loop iterates. If the last row used is 55, then i starts as 55, then iterates to 54, then 53, etc... all the way to 1
For i = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1 'Loops through the last row to row 1 (Step -1 means that i will increment by negative 1 each loop)
'In the above line, this code will find the last used row in a column: "Cells(Rows.Count, 2).End(xlUp).Row" - This is looking at column B (2)

'Start of the If Statements

'If Cell Ai is blank and Cell Bi is blank, then delete the row
    If Cells(i, 1) = "" And Cells(i, 2) = "" Then
        Cells(i, 2).EntireRow.Delete 'Delete the row
        GoTo Nexti 'if the above if statement is true, once the row is deleted this line will force the code to move to the next iteration of the loop (it skips to "Nexti")
    End If

'If Cell Ai is blank, Cell Bi is not blank, Cell Bi is bolded and cell Ai+1 is either Bold or blank then delete the row
    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 Cell Ai is blank, Cell Ai is Bold, and cell Ai+1 is Blank then delete the row
    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: 'This is where "GOTO Nexti" is skipping to
Next i 'Iterate to the next i and restart the loop

End Sub

I hope this helps
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I added comments to the code to try and explain each line as best as I could:
VBA Code:
Sub Remove()

'The goal of this is to loop through each row, and evaluate the 3 "If" statements below. If any of those fire as True, then delete the row and move to the next row.
'I'm using i as an integer that represents each row as the loop iterates. If the last row used is 55, then i starts as 55, then iterates to 54, then 53, etc... all the way to 1
For i = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1 'Loops through the last row to row 1 (Step -1 means that i will increment by negative 1 each loop)
'In the above line, this code will find the last used row in a column: "Cells(Rows.Count, 2).End(xlUp).Row" - This is looking at column B (2)

'Start of the If Statements

'If Cell Ai is blank and Cell Bi is blank, then delete the row
    If Cells(i, 1) = "" And Cells(i, 2) = "" Then
        Cells(i, 2).EntireRow.Delete 'Delete the row
        GoTo Nexti 'if the above if statement is true, once the row is deleted this line will force the code to move to the next iteration of the loop (it skips to "Nexti")
    End If

'If Cell Ai is blank, Cell Bi is not blank, Cell Bi is bolded and cell Ai+1 is either Bold or blank then delete the row
    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 Cell Ai is blank, Cell Ai is Bold, and cell Ai+1 is Blank then delete the row
    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: 'This is where "GOTO Nexti" is skipping to
Next i 'Iterate to the next i and restart the loop

End Sub

I hope this helps
appriecated muchly, this will help my future endevours in VBA :cool:
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,849
Members
449,096
Latest member
Erald

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