VBA对特定人员的付款进行汇总,并将汇总金额设置为合并单元格

我有一个非常大的Excel文件,大约5万行。 在列(C)中,我有人员编号1,1,1,1,2,3,4,5,…和列(N)我有这个人的付款,所以数据看起来像

我想要的是从列N中的一个人的所有支付,并在列O中设置结果,然后合并列O.

excel截图

它看起来像你正在寻找内置的Excel SUMIF函数

 =sumif(C:C,"specific person number",N:N) 
 Sub summAndMerge() lastrow = Worksheets("A").Range("A65536").End(xlUp).Row For i = 2 To lastrow If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i, 1).Value <> Cells(i + 1, 1).Value Then intUpper = i Debug.Print ("<> -1 and <> +1 " & intUpper) End If If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i, 1).Value = Cells(i + 1, 1).Value Then intUpper = i Debug.Print ("<> -1 and = +1 " & intUpper & " UPPPER LIMIT") End If If Cells(i, 1).Value <> Cells(i + 1, 1).Value And Cells(i, 1).Value = Cells(i - 1, 1).Value Then Application.DisplayAlerts = False Debug.Print ("<> +1 and = -1:" & i & "LOWER LIMIT") Range(Cells(intUpper, 3), Cells(i, 3)).Merge Cells(intUpper, 3).Value = "=sumif(B" & CStr(intUpper) & ":B" & CStr(i) & ","">0"")" Range(Cells(i, 1), Cells(i, 24)).Borders(xlEdgeBottom).LineStyle = xlDouble End If If Cells(i, 1).Value <> Cells(i + 1, 1).Value And Cells(i, 1).Value <> Cells(i - 1, 1).Value Then Debug.Print ("One Cells: " & i) Range(Cells(i, 1), Cells(i, 24)).Borders(xlEdgeBottom).LineStyle = xlDouble Cells(intUpper, 3).Value = Cells(intUpper, 2).Value End If Next i End Sub