How to sum values in variable range in VBA?

后端 未结 1 1579
逝去的感伤
逝去的感伤 2021-01-14 19:53

I have a table as shown below.

In column C I would like to Sum values from column A if they have the same index (column B). I would like to put sum result for all t

相关标签:
1条回答
  • 2021-01-14 20:28

    Here's one way:

    Sub example()
        Dim ws                    As Worksheet
        Dim LastRow               As Long
    
        Set ws = ActiveWorkbook.Sheets("Sheet2")
    
        LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    
        With ws.Range("C3:C" & LastRow)
            .ClearContents
            .Value = ws.Evaluate("INDEX(SUMIF(B3:B" & LastRow & ",B3:B" & LastRow & ",A3:A" & LastRow & "),)")
        End With
    End Sub
    
    0 讨论(0)
提交回复
热议问题