如何求和并删除2列上的重复项

时间:2019-10-07 03:42:04

标签: excel vba

Application Match对于一列有效,但是对于两列却给我错误

With Sht
    LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row

    For i = LastRow To 2 Step -1
        DupRow = Application.Match(Cells(i, 9).Value, Range(Cells(1, 9), Cells(i - 1, 9)), 0)
        DoEvents

        If Not IsError(DupRow) Then
            Cells(i, 8).Value = Cells(i, 8).Value + Cells(DupRow, 8).Value
            Cells(i, 9).Value = Cells(i, 9).Value + Cells(DupRow, 9).Value
            Rows(DupRow).Delete

        End If
    Next i
End With

2列错误运行时1004

DupRow = Application.Match(Cells(i, 4).Value & Cells(i, 5).Value, Range(Cells(1, 4) & Cells(1, 5), Cells(i - 1, 4) & Cells(i - 1, 5)), 0)

正确的做法是什么?

1 个答案:

答案 0 :(得分:0)

我认为最好使用字典,字典具有内置的密钥管理方法,因此可以用来获取唯一值:


    def criminals(request):
        if request.method=="POST":
            cn = request.POST['crname']
            ccrime = request.POST['crime']
            cage = request.POST['age']
            cheight=request.POST['height']
            cbody = request.POST['bodymark']
            crgen = request.POST['gender']
            s= Criminals()
            s.mname=cn
            s.mcrime=ccrime
            s.mage=cage
            s.image = request.FILES['photo']
            s.mheight=cheight
            s.mbody=cbody
            s.mgender=crgen
            s.save()
            messages.success(request,"Criminal Added Successfully.")
            return render(request,'criminal.html')
        else:
            return render(request,'criminal.html')