Classifications

2022

Board Regular
Joined
Jun 5, 2022
Messages
74
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet where the classifications of some products are incorrect.

I've re-created a simple example below.

Context:

Column A has a list of Supermarkets that supply products.

Column B has the ID for those products.

Column C has the product description.

Column D has the classification.

In this instance, there are two products with multiple classifications.

In row 5, the classification for the Apple (product ID 1) is 'Vegetable' but it should only be fruit!

In row 7, the classification for the Kale (product ID 3) is 'Fruit' but it should only be Vegetable!

The real data has thousands of rows with instances like this, where an ID has multiple classifications, when each ID should only have ONE classification (and there are about a dozen classifications).

Is there a quick way to identify all the instances in the table where the ID column has MULTIPLE classifications in column D, but also find out what those multiple classifications are?

Grocery StoreIDProductClassification
Kroger
1​
AppleFruit
Tesco
2​
PearFruit
Boots
3​
KaleVegetable
Costco
1​
AppleVegetable
Carrefour
2​
PearFruit
Walmart
3​
KaleFruit
 
Or just apply conditional formatting on sorted B values without the helper column(s)?
1694285216369.png

CF rule: =AND($B2=$B3,$D2<>$D3)

Not sure what the effect would be if you had 1,1,1 and Fruit, Vegetable, Grain
Edit -
Answer:
1694285733100.png
 
Last edited:
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I’d like to take this a bit further….

I need to isolate all the IDs that multiple classifications - so in the example above, I’d need to filter by all rows with the ID 1 and all rows with the ID 3.

Is there a way to do that, without manually selecting each ID?
 
Upvote 0
I responded to another solution just now.

But would also like to respond to the one I created above.

Is there a way to filter by all the IDs that have multiple classifications?

So in the data set above, is there a way to filter for all rows which contain ID 1 and ID 3 (which both have multiple classifications), without having to manually select each ID?
 
Upvote 0
Try this macro:
VBA Code:
Sub FilterProducts()
    Application.ScreenUpdating = False
    Dim v() As Variant, lRow As Long, i As Long, x As Long, rng As Range, srcRng As Range
    Dim dic As Object, arr() As Variant, cnt As Long, val As String
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("B2:B" & lRow).Resize(, 2).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            Range("A1").AutoFilter 2, v(i, 1)
            Set srcRng = Range("D2:D" & lRow).SpecialCells(xlVisible)
            val = srcRng.Cells(1, 1)
            For Each rng In srcRng
                If rng = val Then cnt = cnt + 1
            Next rng
            If cnt <> [subtotal(103,A:A)] - 1 Then
                x = x + 1
                ReDim Preserve arr(1 To x)
                arr(x) = v(i, 1)
            End If
        End If
        cnt = 0
    Next i
    Range("A1").AutoFilter
    Cells(1, 1).Sort Key1:=Columns(2), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes
    Range("A1").AutoFilter Field:=2, Criteria1:=arr, Operator:=xlFilterValues
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this macro:
VBA Code:
Sub FilterProducts()
    Application.ScreenUpdating = False
    Dim v() As Variant, lRow As Long, i As Long, x As Long, rng As Range, srcRng As Range
    Dim dic As Object, arr() As Variant, cnt As Long, val As String
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("B2:B" & lRow).Resize(, 2).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v) To UBound(v)
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            Range("A1").AutoFilter 2, v(i, 1)
            Set srcRng = Range("D2:D" & lRow).SpecialCells(xlVisible)
            val = srcRng.Cells(1, 1)
            For Each rng In srcRng
                If rng = val Then cnt = cnt + 1
            Next rng
            If cnt <> [subtotal(103,A:A)] - 1 Then
                x = x + 1
                ReDim Preserve arr(1 To x)
                arr(x) = v(i, 1)
            End If
        End If
        cnt = 0
    Next i
    Range("A1").AutoFilter
    Cells(1, 1).Sort Key1:=Columns(2), Order1:=xlAscending, Orientation:=xlTopToBottom, Header:=xlYes
    Range("A1").AutoFilter Field:=2, Criteria1:=arr, Operator:=xlFilterValues
    Application.ScreenUpdating = True
End Sub
Thanks for your repsonse, mumps.

I tried it and it partially worked. But it only returned one row with the '1' ID when it should have returned multiple rows with the '1' ID and also the '3' ID....

So it returned this:

Grocery StoreIDProductClassificationChecks?
Kroger1AppleFruitCONSECUTIVENot ok


But was missing the other row with the 1 ID below and the 3 IDs....


Grocery StoreIDProductClassificationChecks?
Kroger
1​
AppleFruitCONSECUTIVENot ok
Costco
1​
AppleVegetable
Tesco
2​
PearFruitCONSECUTIVEOk
Carrefour
2​
PearFruit
Boots
3​
KaleVegetableCONSECUTIVENot ok
Walmart
3​
KaleFruit
Carrefour
4​
BananaFruitCONSECUTIVEOk
Tesco
4​
BananaFruit
 
Upvote 0
This is what I get when I run the macro:
Book1
ABCDEF
1Grocery StoreIDProductClassificationChecks?
2Kroger1AppleFruitCONSECUTIVENot ok
3Costco1AppleVegetable
6Boots3KaleVegetableCONSECUTIVENot ok
7Walmart3KaleFruit
Sheet1

If it is not working for you, could you upload a copy of your actual file (de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here.
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,092
Members
449,095
Latest member
gwguy

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