突出显示列中的重复,同时需要列T中的值

时间:2017-12-22 07:11:46

标签: excel excel-vba vba

我写了一些代码,当A列中有Duplicate值时,那么True = False就会出现,就像我们在Excel中一样,A1 = A2,直到最后我们在A中有数据的行。

我不确定如何查找最后一行,因此我将范围编码为T9000,但最多可能有T3500或有时T15000的数据。

Range("A1:A5000").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
ActiveCell.FormulaR1C1 = "Dup"
Range("T2").Select
ActiveCell.FormulaR1C1 = "=R[-1]C[-19]=RC[-19]"
Range("T3").Select
Selection.End(xlDown).Select
Range("S1048576").Select
Selection.End(xlUp).Select
Range("T9000").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown

1 个答案:

答案 0 :(得分:0)

我不确定你的Dup的放置应该是A1单元格吗?可以使用最后一行变量来缩小代码的其余部分。

lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row

合同代码:

Option Explicit

Sub FindDups()

    Dim wb As Workbook
    Dim wsSource As Worksheet
    Dim lastRow As Long

    Set wb = ThisWorkbook                        'Variable assignments
    Set wsSource = wb.Worksheets("Sheet2")

    lastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row 'find last row by coming from the bottom of the sheet and finding last used cell in column

    With wsSource
        .Range("A1").FormulaR1C1 = "Dup"
        .Range("A1:A" & lastRow).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
        .Range("T2:T" & lastRow).FormulaR1C1 = "=IF(RC[-19]=R[-1]C[-19],""Duplicate"", RC[-19])"
    End With

End Sub