title: VBA: loop through rows, sum up certain rows if condition is met
body: <p>I have been using the forums for a couple of weeks now while learning VBA. But now I am stuck and need help. So here is my problem:</p>
  
  <p>I have a workbook with several worksheets in it. For simplicity let's assume that I have two worksheets. One with a lot of raw data material and one which is nicely formatted and will be the receiving sheet.</p>
  
  <p>The raw data table looks like this: I have 8 columns and I need to check for two conditions in each row (columns B and E) and sum up the values in column H. 
  Column B contains different names and column E contains a working status, e.g. ready or talking. I do have about 3.000 rows and a set of about 20 different names with different status. So overall I have, let's say 100 rows with the name "Smith" and the status "talking" in no particular order. I now need to sum up all the numbers (column H) in each of those rows where the name "Smith" and the status "talking" are put in. After that, I would like to have the name (column B), the status (column E) and the sum (column H), to be written into cells of the same workbook in a dynamic range, starting at I3:K3. I have to admit that I am totaly in the dark on this one..
  I really hope that you guys can help me with some code and/or point me into the right direction. Most of all I hope that you could also explain what your code does, so I can understand it properly and learn. Thanks in advance guys!</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>Private Sub SumNumbers()
  
  Dim tbl As Worksheet
  Dim x As Integer
  Dim lrow As Long
  Dim lastname As String
  Dim astatus As String
  Dim number As Integer
  Dim counter As Integer
  
  Set wb = ThisWorkbook
  Set tbl = wb.Sheets("Sheet1")
  lrow = tbl.Cells(Rows.Count, "B").End(xlUp).Row
  lastname = tbl.Cells("C2:C" &amp; lrow).Text
  astatus = tbl.Cells("E2:E" &amp; lrow).Text
  number = tbl.Cells("H2:H" &amp; lrow)
  
  For x = 2 To lrow
  
      If lastname = "Smith" And astatus = "Talking" Then
          counter = counter + number
      End If
  
  Next x
  
  End Sub
  </code></pre>