Sum Column B based on Column A using Excel VBA Macro

后端 未结 6 696
清酒与你
清酒与你 2021-01-17 00:59

OK, I have a simple problem that I need help with in a VBA Macro. I have an excel sheet that looks like this...

Product #     Count
101              1
102          


        
6条回答
  •  情歌与酒
    2021-01-17 01:54

    Sub BestWaytoDoIt()
    
    Dim i As Long                    ' Loop Counter
    Dim int_DestRwCntr As Integer    ' Dest. sheet Counter
    Dim dic_UniquePrd As Scripting.Dictionary
    Set dic_UniquePrd = New Scripting.Dictionary
    
    For i = 2 To Sheet1.Range("A" & Sheet1.Cells.Rows.Count - 1).End(xlUp).Row
        If dic_UniquePrd.exist(Sheet1.Range("A" & i).Value) <> True Then
            dic_UniquePrd.Add Sheet1.Range("A" & i).Value, DestRwCntr
            sheet2.Range("A" & int_DestRwCntr).Value = Sheet1.Range("A" & i).Value
            sheet2.Range("B" & int_DestRwCntr).Value = Sheet1.Range("B" & i).Value
        Else
            sheet2.Range("A" & dic_UniquePrd.Item(Sheet1.Range("A" & i).Value)).Value = sheet2.Range("B" & dic_UniquePrd.Item(Sheet1.Range("A" & i).Value)).Value + Sheet1.Range("B" & i).Value
        End If
    Next
    End Sub
    

    This will serve the purpose.. Only thing to remember is to activate "Microsoft Scripting Runtimes" in references.

提交回复
热议问题