Forum Discussion

TimBorm's avatar
TimBorm
Copper Contributor
Feb 09, 2020

VBA sumif code

Hi All, 

 

please help with macro that will look for the same value in column and then sum value from another column.

ex. 

idqytsum
111111114 
222222256 
3333333534 
111111123 
222222243 
33333334 

 

thank you in advance πŸ™‚ 

best regards 

michal

3 Replies

  • Savia's avatar
    Savia
    Iron Contributor
    Is there some reason this needs to be done in VBA? Either an actual SUMIF function or a PivotTable would be much, much easier and more reliable.
    • TimBorm's avatar
      TimBorm
      Copper Contributor

      For next purpose it need to be done in VBA, but i think that i found the easiest way:

      Option Explicit

      Sub CreateUniqueList()
      Dim lastrow As Long

      lastrow = Cells(Rows.Count, "B").End(xlUp).Row

      ActiveSheet.Range("B2:B" & lastrow).AdvancedFilter _
      Action:=xlFilterCopy, _
      CopyToRange:=ActiveSheet.Range("D2"), _
      Unique:=True

      End Sub

       

      Savia 

      • TimBorm's avatar
        TimBorm
        Copper Contributor
        my mistake :D:D this one:
        Sub sumifFunction()
        Range(β€œG2”) = β€œA”
        Range(β€œH2”) = Application.WorksheetFunction.SumIf(Range(β€œA2:B8”), β€œA”, Range(β€œB2:B8”))
        End Sub

Resources