Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在同一列中找到每个重复项的最后一行索引?_Excel_Vba - Fatal编程技术网

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

Excel 如何在同一列中找到每个重复项的最后一行索引?,excel,vba,Excel,Vba,我试图创建一个程序,这样它就可以找到位于同一列中的每个副本的最后一行索引,并存储它们的值。例如,在图片中,约翰、特朗普、爱丽丝和莎拉的最后一行名字索引应该分别为13、17、23、26。目前,我的代码只能识别重复项,因此我可以做些什么来查找每个重复项的最后一行索引,不仅针对我显示的图片,而且针对所有情况 可以通过多种方式完成。在下面的代码(已测试)中使用了dictionary对象。请添加工具->参考->Microsoft脚本运行时 Sub Testing() Dim mycell As

我试图创建一个程序,这样它就可以找到位于同一列中的每个副本的最后一行索引,并存储它们的值。例如,在图片中,约翰、特朗普、爱丽丝和莎拉的最后一行名字索引应该分别为13、17、23、26。目前,我的代码只能识别重复项,因此我可以做些什么来查找每个重复项的最后一行索引,不仅针对我显示的图片,而且针对所有情况


可以通过多种方式完成。在下面的代码(已测试)中使用了dictionary对象。请添加工具->参考->Microsoft脚本运行时

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

修改代码以提供范围对象(从注释中可以理解)

谢谢您的帮助,但是我如何使用字典中存储的最后一行值来表示不同的名称?不清楚您希望如何使用这些值?dict对象的每个键都是名称,“dict(key)”给出最后一个行号。因为我需要使用字典中存储的行索引来执行某些操作,可能类似于复制名为trump的范围,所以我需要基于dictorial中存储的行索引执行循环,例如i=lastrowjohn+1到lastrowTrump。。。但是,在这种情况下,以Dict的形式,我不知道如何表达它的担忧,如果它不符合主题,是否也可以存储我找到的重复项的名称,并将它们放入变量中,或者更好地放入数组中?因此,我可以将它们表示为数组,比如Dim-Arr:Arr=array(“,”,“,”,”,“,”)(不要直接将它们的名称放入数组中,因为您可以假设我不确定在开始时复制的名称是谁…)
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