Excel VBA 代码,如果在另一个特定列中发现重复项,则在特定列中添加重复项

问题描述 投票:0回答:1

我必须在特定列中查找重复项,如果找到重复项,则将“重复项”用于另一列中的重复记录。 例如,我要查找重复项的列是“F”,数字必须添加到“CG”列中。 你能帮帮我吗?

Sub sbFindDuplicatesInColumn_C()

Dim ws As Worksheet

  'Set reference to the sheet in the workbook.
  Set ws = ThisWorkbook.Worksheets("Master")
  ws.Activate 'not required but allows user to view sheet if warning message appears
  
'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

'Finding the last row in the Column 1
    lastRow = Range("F99999").End(xlUp).Row

'looping through the column1
    For iCntr = 1 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("F1:F" & 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, 80) = "Duplicate"
            End If
        End If
    Next
End Sub
excel vba duplicates
1个回答
0
投票

Sub sbFindDuplicatesInColumn_C()

将 ws 调暗为工作表

'设置对工作簿中工作表的引用。 设置 ws = ThisWorkbook.Worksheets("Master") ws.Activate '不需要,但如果出现警告消息则允许用户查看工作表

'将 lastRow 变量声明为 Long 以将最后一行值存储在 Column1 中 将 lastRow 变暗

'matchFoundIndex是存放给定值的匹配索引值 Dim matchFoundIndex As Long

'iCntr是用For循环遍历第1列的所有记录 将 iCntr 变暗

'查找第 1 列的最后一行 lastRow = 范围("F99999").End(xlUp).Row

'循环遍历column1 对于 iCntr = 1 到 lastRow '检查单元格是否有任何项目,如果它是空白则跳过。 如果单元格(iCntr,6)<>“”那么 '获取单元格值的匹配索引号 matchFoundIndex = WorksheetFunction.Match(单元格(iCntr, 6), Range("F1:F" & lastRow), 0) '如果匹配索引不等于当前行号,那么它是一个重复值 如果 iCntr <> matchFoundIndex 那么 '在B列打印标签 细胞(iCntr,85)=“重复” 万一 万一 下一个 结束子

© www.soinside.com 2019 - 2024. All rights reserved.