我有超过30k行数据的电子表格,每行有38个数据点。为了确定是否有任何重复的行,我连接了每行的14个字段,并修剪了数据。这为每行包含349个字符创建了一个键。
编辑:当所有14个字段完全相同时,会发生密钥的重复。从视觉评论中,我已经确定了大约200行(2000年),其中所有14个字段中的数据完全相同。
然后,我使用application.match运行搜索,以确定是否已创建该密钥。当没有重复时,application.match函数工作正常。但是,当存在重复时,它会返回
错误2015
我知道这是函数返回#VALUE的时候。当我使用立即窗口检查时,值是重复的,当我在电子表格中键入“= Xi = Yj”时,它表示相同(即TRUE)
因为我需要知道项目下一部分的正确行值,我想知道是否有任何方法来识别重复值,并获得正确的行值?
我的代码如下(仍然只是一个PoC):
While Cells(a, 1) <> vbNullString
If Cells(a, 36).Interior.Color = 5296274 Then
ElseIf Cells(a, 36) <> vbNullString Then
Else
Cells(a, 39) = Trim(Cells(a, 3) & Cells(a, 4) & Cells(a, 5) & Cells(a, 6) & Cells(a, 7) & Cells(a, 8) & Cells(a, 9) & Cells(a, 10) & _
Cells(a, 11) & Cells(a, 12) & Cells(a, 13) & Cells(a, 14) & Cells(a, 15) & Cells(a, 16) & _
Cells(a, 17) & Cells(a, 18) & Cells(a, 19))
Cells(a, 44) = Len(Cells(a, 39))
m = vbNullString
m = Application.Match(Cells(a, 39), Range(Cells(2, 39), Cells(a - 1, 39)), 0)
If m = vbNullString Then
Cells(a, 39).Select
Selection.Style = "Good"
Else
' Range(Cells(a - 1, 39), Cells(a, 39)).Select
' Selection.Style = "Bad"
End If
End If
a = a + 1
Wend
尝试将错误嵌入条件中,有两个选项。像这样:
If IsError(Application.Match(Cells(a, 39), Range(Cells(2, 39), Cells(a - 1, 39)), 0)) Then
'm = something
Else
m = Application.Match(Cells(a, 39), Range(Cells(2, 39), Cells(a - 1, 39)), 0)
End If
经过大量的实验,我想出了错误的原因。
正在创建的密钥长度为349个字符。许多Excel的功能的最大字符限制为256(0 - 255)。
基于此,我使用trim功能切断了按键。
代码现在看起来像:
While Cells(a, 1) <> vbNullString
If Cells(a, 36).Interior.Color = 5296274 Then
ElseIf Cells(a, 36) <> vbNullString Then
Else
' Cells(a, 39) = Join(Application.Index(Cells(a, 3).Resize(, 17).Value, 0, 0), ";")
Cells(a, 39) = Trim(Cells(a, 3)) & Trim(Cells(a, 4)) & Trim(Cells(a, 5)) & Trim(Cells(a, 6)) & Trim(Cells(a, 7)) & Trim(Cells(a, 8)) & Trim(Cells(a, 9)) & Trim(Cells(a, 10)) & _
Trim(Cells(a, 11)) & Trim(Cells(a, 12)) & Trim(Cells(a, 13)) & Trim(Cells(a, 14))
' & Trim(Cells(a, 15)) & Trim(Cells(a, 16)) & Trim(Cells(a, 17)) & Trim(Cells(a, 18)) & Trim(Cells(a, 19))) removed
Cells(a, 44) = Len(Cells(a, 39))
m = vbNullString
m = Application.Match(Cells(a, 39), Range(Cells(1, 39), Cells(a - 1, 39)), 0)
If m = vbNullString Then
Cells(a, 39).Select
Selection.Style = "Good"
' Cells(a, 41).Select
' Selection.ClearContents
Else
Cells(a, 39).Select
Selection.Style = "Bad"
Cells(a, 40) = m
Cells(m, 39).Select
Selection.Style = "Bad"
Cells(m, 40) = a
End If
End If
a = a + 1
Wend