【问题标题】:VBA - IF loop improvementsVBA - IF 循环改进
【发布时间】:2016-07-29 14:59:15
【问题描述】:

我目前正在运行一个宏,它标识工作簿中的重复项,但是它标识了索引中的第一个集合并且没有标记第一个集合,这导致我设置了一个 if 语句来绕过这个,这也将重复添加到第一个实例。然而,这需要很长时间才能完成,如果可能的话,我们希望对此进行改进。任何建议都将不胜感激,我是 VBA 新手,但在遇到新问题时一直在学习!

'Declaring the lastRow variable as Long to store the last row value in the Column1
Dim lastRow As Long
'matchFoundIndex is to store the match index values of the given value
Dim matchFoundIndex As Long
'iCntr is to loop through all the records in the column 1 using For loop
    Dim iCntr As Long
    Dim first_dup As Long
    Dim tagging As Long
    Dim item_code As String

'Finding the last row in the Column 1
    lastRow = Range("B1000000").End(xlUp).Row
'
'looping through the column1
    For iCntr = 2 To lastRow

        'checking if the cell is having any item, skipping if it is blank.
        If Cells(iCntr, 1) <> "" Then
             'getting match index number for the value of the cell

             matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)

            'if the match index is not equals to current row number, then it is a duplicate value
            If iCntr <> matchFoundIndex Then

                'Printing the label in the column B
                Cells(iCntr, 4) = "Duplicate"

            End If
        End If
    Next


   For first_dup = 2 To lastRow
      If Cells(first_dup, 5) = "Duplicate" Then
          item_code = Cells(first_dup, 1)
          For tagging = 2 To lastRow
              If Cells(tagging, 1) = item_code Then
                   Cells(tagging, 5) = "Duplicate"
              End If
          Next
       End If
   Next

Example data:
item code   
1   
2   
3   
4   
1   duplicate
2   duplicate
3   duplicate
4   duplicate
1   duplicate
2   duplicate
3   duplicate
4   duplicate

【问题讨论】:

  • 迭代 Range.Value 数组而不是使用单个 Range/Cell 应该会有所帮助。您也可以使用公式=IF(MATCH($A2,$A:$A,0)&lt;&gt;ROW($A2), "Duplicate", "") 获得相同的结果。

标签: vba loops if-statement


【解决方案1】:

我的第一个建议是不要让事情过于复杂,尝试使用重复值条件格式,看看是否有帮助:

如果您不希望仅找到重复项而不是第一次出现,则可以使用如下公式:(在单元格 B2 中,如果您的数据从 A2 开始,则需要一个标题行t 匹配,否则您的第一行将始终匹配)

=IF(COUNTIF($A1:A$1,A2)>=1,"Duplicate","")

粘贴下来的数据行可能如下所示:

如果您迫切需要 VBA 解决方案,也有 VBA 解决方案,但我想我会先为您提供简单的解决方案。让我知道你在 cmets 中的表现。

编辑:您可以使用 VBA 插入上述公式,使用 R1C1 表示法,例如:

Sub test()
    Range("B2:B" & Range("A1").End(xlDown).Row).FormulaR1C1 = "=IF(COUNTIF(R1C1:R[-1]C1,RC1)>=1,""Duplicate"","""")"
End Sub

我将对此进行分解,以便您知道发生了什么。

Range("B2:B" &amp; Range("A1").End(xlDown).Row) 在 B2 和 A 列中最后填充的行之间选择 B 列中的单元格,即 Range("A1").End(xlDown).Row(因此,如果您希望 A 列中的空白作为数据的一部分,这将不起作用)

然后,它将 R1C1 ref 公式设置为"=IF(COUNTIF(R1C1:R[-1]C1,RC1)&gt;=1,""Duplicate"","""")",其中R1C1 表示第一行第一列,(即$A$1R[-1]C1 表示上一行,第一列。例如, 如果您在 B5,这将选择 A4。 如果您在 A2 中,这将选择 A1。 如果您在 A1 中,这将出错,因为您不能早于 1 排在一行中。 而RC1 表示当前行第一列。

希望这会有所帮助!

【讨论】:

  • 嗨 Andrew,我很想找到所有重复项,但它需要用“重复”标签标记它们,因为我会进一步处理以查看重复项所在的类别。这需要成为一个 vba 解决方案,因为我实际上不会继续运行它,其他人也不知道如何找到重复项。首先感谢您对excel的建议!格德
  • 你好,Andrew,我现在只是在尝试你的建议,虽然 countif 确实需要很长时间,但我认为它会比我之前做的循环更好。我最初设计了循环,因为似乎没有标记所有我需要的重复项。我使用它来进一步查找哪些项目列表彼此重叠。感谢您的建议!
  • 这回答了您的问题吗?如果是这样,您能否将其标记为已回答?
【解决方案2】:

答案与我提供的初始代码相同,30000 个项目大约需要 5 分钟,所以它的功能还不错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2013-04-11
    • 2013-12-29
    • 2019-11-03
    • 2017-07-22
    相关资源
    最近更新 更多