title: VBA SUMIF based on Variables
body: <p>I'm new to VBA and trying to figure this one out, I did not work with functions yet.
  I'm trying to automate the SUMIF formula on my VBA sheet. The Rec sheet will contain a dynamic SUMIF formulas based on a variable/range in Column D based on Column B values. the SUM range will be coming from the CB sheet, matching column K and summing column L. 
  The following code does not return any values for the SUMIF functions, no errors to report.</p>
  
  <p>Data Sample in the image below:</p>
  
  <p><a href="https://i.stack.imgur.com/VvDfw.png" rel="nofollow noreferrer">https://i.stack.imgur.com/VvDfw.png</a></p>
  
  <pre><code>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  
      Dim ws As Worksheet
      Dim NoCol As Integer, NoRow As Integer
      Dim CritRng As Range, SumRng As Range
  
      Application.ScreenUpdating = False
  
      Set ws = Worksheets("Rec")
  
      With ws
          NoRow = .Cells(.Cells.Rows.Count, 3).End(xlUp).Row
          NoCol = .Cells(3, .Cells.Columns.Count).End(xlToLeft).Column
          Set CritRng = Sheets("CB").Range("k:k")
          Set SumRng = Sheets("CB").Range("L:L")
      End With
  
      For r = 3 To NoRow
          Cells(r, NoCol) = WorksheetFunction.SumIf(CritRng, Cells(r, 1), SumRng)
      Next r
  
      Application.ScreenUpdating = True
  
  End Sub
  ```
  
  
  </code></pre>