首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在同一列中找到每个重复项的最后一行索引?

问如何在同一列中找到每个重复项的最后一行索引?
EN

Stack Overflow用户
提问于 2020-03-26 04:00:38
回答 2查看 366关注 0票数 2

我正在尝试创建一个程序,以便它能够找到位于同一列中的每个重复项的最后一行索引,并存储它们的值。例如,在图片中,约翰、王牌、爱丽丝和莎拉的最后一行名字索引应该分别给我13,17,23,26。目前,我的代码只能识别副本,所以我可以做什么来找到每个重复的最后一行索引,不仅是我显示的图片,而且所有的情况都是如此?

​

​

代码语言:javascript
复制
Sub Testing()

    Dim mycell As Range, RANG As Range

    With Sheets(1)

        ' Build a range (RANG) between cell F2 and the last cell in column F
        Set RANG = Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))

    End With

    ' For each cell (mycell) in this range (RANG)
    For Each mycell In RANG

        ' If the count of mycell in RANG is greater than 1, then set the value of the cell 1 across to the right of mycell (i.e. column G) as "Duplicate Found"
        If Application.WorksheetFunction.CountIf(RANG, mycell.Value) > 1 Then

        'how do i find the last row index of each duplicate here?

    Next mycell

End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-26 05:34:38

有很多种方法。下面代码(测试)中使用的字典对象。请添加工具->参考->微软脚本运行时。

代码语言:javascript
复制
Sub Testing()
    Dim mycell As Range, RANG As Range, Dict As Dictionary, Mname As String, Rng As Range
    Set Dict = New Dictionary
    With Sheets(1)
        ' Build a range (RANG) between cell F2 and the last cell in column F
        Set RANG = Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp))
    End With


    ' For each cell (mycell) in this range (RANG)
    For Each mycell In RANG
        Mname = mycell.Value
        ' If the count of mycell in RANG is greater than 1, then set the value of the cell 1 across to the right of mycell (i.e. column G) as "Duplicate Found"
        If Application.WorksheetFunction.CountIf(RANG, mycell.Value) > 1 Then


        If Dict.Count > 0 And Dict.Exists(Mname) Then
        Dict(Mname) = mycell.Row()
        Else
        Dict.Add Mname, mycell.Row()
        End If

        End If
    Next mycell


    'Display result in debug window (Modify to your requirement)
    Startrow = 2
    For Each Key In Dict.Keys
    Set Rng = Sheets(1).Range("A" & Startrow & ":A" & Dict(Key))
    Startrow = Dict(Key) + 1
    ' Now may copy etc the range Rng
    Debug.Print Key, Dict(Key), Rng.Address
    Next



End Sub

修改以给出范围对象的代码(从注释中理解)

票数 1
EN

Stack Overflow用户

发布于 2021-10-19 08:29:37

  1. 创建一个下拉列表,其中包含b1 (通过数据验证)

中的所有名称

  1. In c1粘贴以下函数(以显示最后一个值出现的行号)

代码语言:javascript
复制
{=MAX(($A$1:$A$26=$B$1)*ROW($A$A:$A$226))}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60860839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档