指示具有多个对应值的单元格

时间:2017-10-06 16:29:03

标签: excel excel-vba vba

我目前有一个Excel数据集,显示在不同时间销售的产品以及销售人员如何指定所售商品的产品类别。

Product#    Product Class

10001       Hardware
20002       Software
30003       Misc.
10001       Hardware
10001       Software
20002       Software
10001       Hardware
30003       Misc.

您会注意到,对于产品#10001(应归类为"硬件"),销售人员错误地将其中一个销售额指定为"软件。"

因此,如果我按产品#10001过滤,我将获得2个不同的产品类。 我试图找出哪个产品#有超过1个产品类别,可用于约30,000种不同的产品,以便它们可以修复。

如何查看数据以指明哪些产品编号需要修复为只有1个产品类别?

我只期待存在多个类别指定问题的少数情况,因此一旦我找出哪个产品#有问题,就可以手动修复。

谢谢!

3 个答案:

答案 0 :(得分:1)

您可以在单元格=IF(COUNTIF($A$3:$A$1000,A3)-COUNTIFS($A$3:$A$1000,A3,$B$3:$B$1000,B3)>0,"Wrong","OK")中使用此公式C3。根据需要调整范围并将公式拖到底部。

enter image description here

答案 1 :(得分:1)

您可以使用称为dictionary的对象。字典具有键和对应的值。实现密钥的存储方式,以便快速查明字典中是否存在特定密钥,这有助于检查重复的产品代码。

为简单起见(因为我对Excel中的VBA经验不多),我假设要检查的数据位于Sheet(1)的第1列和第2列中:

Option Explicit

' Add reference to get Dictionary: http://www.techbookreport.com/tutorials/vba_dictionary.html
' Excel VBA- Finding the last column with data: https://stackoverflow.com/a/11927387

Sub FindDuplicates()
    Dim dict As Dictionary
    Set dict = New Dictionary

    Dim ws As Worksheet
    Set ws = Sheets(1)

    Dim rLastCell As Range
    Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
                                  xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)

    Dim key As String
    Dim val As String
    Dim dupes As String
    Dim i As Long
    ' use maxFinds to limit the number of duplicates found
    Dim maxFinds As Integer
    maxFinds = 32
    Dim nFound As Integer
    nFound = 0

    For i = 1 To rLastCell.Row
        key = Cells(i, 1).Value
        val = Cells(i, 2).Value
        If dict.Exists(key) Then
            If dict(key) <> val Then
                dupes = dupes & "Row: " & i & " Class: " & val & vbCrLf
                nFound = nFound + 1
            End If
        Else
            dict.Add key, val
        End If
        If nFound = maxFinds Then
            Exit For
        End If
    Next

    If nFound = 0 Then
        MsgBox ("No duplicates found.")
    Else
        MsgBox (dupes)
    End If

End Sub

字典对象不是内置于Excel中的,因此您需要通过&#34;工具&#34;添加对Microsoft Scripting Runtime的引用。菜单 - &gt; &#34;引用...&#34;

我创建了一个包含50,000行和大量重复项的测试文件,这就是为什么代码最终会设置限制找到重复项的数量,因此您可以设置maxFinds = 10并完成这些操作,然后再次运行宏以找到另外十个,依此类推。此外,如果有多于32个,那么他们无论如何都不适合在消息框中。

它假定第一次出现&#34;产品类&#34; (或上面代码示例中第2列中的值)是正确的。

示例输出:

enter image description here

答案 2 :(得分:0)

您可以使用此数组公式(单击 Ctrl + Shift + 输入)以找出需要处理的数组:

tail

来自=IF(IFERROR(VLOOKUP(A2,$A$1:A1,1,0),0)>0,IF(ISTEXT(VLOOKUP(A2&B2,$A$1:A1&$B$1:B1,1,0)),"","Dup"),"") 的公式并向下拖动。

然后只需使用 Dup 字样进行过滤。这应该适合你。

相关问题