title: Excel VBA script to filter and sum
body: <p>I am using the code below and it works for filtering the unique name and totalting the value field.  I recently have a need to expand upon the filtering of unique names to include other columns in the criteria.  Please see the example output I am looking for.  Any help would be appreciated.</p>
  
  <pre><code> Sub SUM()
  
    Dim i, j, k As Integer
     i = 2
     j = 2
  
  Range("D1").Value = "NAME"
  Range("E1").Value = "VALUE"
  
  'copy the first value of column A to column D
  Range("D2").Value = Range("A2").Value
  
  'cycle to read all values of column B and sum it to column E; will run until find a blank cell
  While Range("A" &amp; i).Value &lt;&gt; ""
  
      'this check if actual value of column A is equal to before value of column A, if true just add the column B value to E
      'else, look for the row in column D where is the same value of column A, if it doesn't exist code create the value
      'in column D and E
      If Range("A" &amp; i).Value = Range("A" &amp; i - 1).Value Then
          Range("E" &amp; j).Value = Range("E" &amp; j).Value + Range("B" &amp; i).Value
      Else
          flag = 1
          While Range("D" &amp; flag).Value &lt;&gt; ""
              If Range("A" &amp; i).Value = Range("D" &amp; flag).Value Then
                  j = flag
                  Range("E" &amp; j).Value = Range("E" &amp; j).Value + Range("B" &amp; i).Value
                  flag = Range("D1").End(xlDown).Row
              Else
                  j = 0
              End If
              flag = flag + 1
          Wend
          If j = 0 Then
              Range("D1").End(xlDown).Offset(1, 0).Value = Range("A" &amp; i).Value
              Range("E1").End(xlDown).Offset(1, 0).Value = Range("B" &amp; i).Value
              j = Range("E1").End(xlDown).Row
          End If
      End If
  
      i = i + 1
  Wend
  MsgBox "End"
  
  End Sub
  </code></pre>
  
  <p>Currently outputs like this:</p>
  
  <pre><code> Name  Value       Name    Sum
      A           1       A     13
      A           2       B      7
      B           1       C      3
      B           3           
      C           2           
      A           1           
      B           2           
      A           3           
      B           1           
      A           2           
      A           4           
      C           1      
  </code></pre>
  
  <p>I would like to have it export data like this example:</p>
  
  <pre><code>Name  Code  Date     Value       Name   Code  Date   Sum
     A   101  3/10/17      1       A     101   3/10/17    9
     A   101  3/10/17      2       A     102   3/10/17    4
     B   102  3/10/17      1       B     101   3/10/17    3
     B   101  3/10/17      3       B     102   3/10/17    2
     C   102  3/8/17       2       B     101   3/8/17     2
     A   102  3/10/17      1       C     102   3/8/17     2
     B   101  3/8/17       2       C     102   3/10/17    1   
     A   102  3/10/17      3         
     B   102  3/10/17      1           
     A   101  3/10/17      2           
     A   101  3/10/17      4           
     C   102  3/10/17      1           
  </code></pre>