title: Create a custom SUM function with VBA
body: <p>I have a table called (&quot;Vendas2020&quot;) that has the daily sales and each row has either &quot;C&quot; or &quot;V&quot; if they're either a purchase or a sale (C/V column is the fifth one and is called &quot;Compra / Venda&quot;). I have inserted a function to only calculate the visible values in the table since the destination worksheet (which is a report called &quot;Relatório&quot;) has slicers for the products, salesperson and year. The code for the sumfunction is as follows:</p>
  <pre><code>Function SUMVisible(Rg As Range)
    Dim xCell As Range
    Dim xCount As Integer
    Dim xTtl As Double
  
    Application.Volatile
    Set Rg = Intersect(Rg.Parent.UsedRange, Rg)
  
    For Each xCell In Rg
      If xCell.ColumnWidth &gt; 0 _
         And xCell.RowHeight &gt; 0 _
         And Not IsEmpty(xCell) _
         And IsNumeric(xCell.Value) _
      Then
        xTtl = xTtl + xCell.Value
        xCount = xCount + 1
      End If
    Next
    If xCount &gt; 0 Then
      SUMVisible = xTtl
    Else
      SUMVisible = 0
    End If
  End Function
  </code></pre>
  <p>I'd like to add an if statement (or something like it) that only sums the values if they're purchases (&quot;V&quot;) but I can't seem to make it work. It either throws a value error or a spill error. I can't seem to find a thread that has a solution that might apply in this case. I know it's something simple but I'm somewhat new to VBA.
  Thanks in advance!</p>