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

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

我写了一些代码,当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
excel excel-vba vba
1个回答
0
投票

如果它是A1单元格,我不确定你的Dup的位置?可以使用最后一行变量来缩小代码的其余部分。

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
© www.soinside.com 2019 - 2024. All rights reserved.