In the spreadsheet “(ACOES) VALORES” I will place a function =QTDE([@ACÃO])
The QTDE function will look in the EXTRATO spreadsheet, the EXTRATO table, the TIPO column and in all cells with COMPRA, BONIFICACAO or SUBSCRICAO values
Where these values were found, you will check the ATIVO column for values that were equal to what you selected in the formula =QTDE([AÇÃO]), when you find identical values, you will sum the values in the QTDE column of the EXTRATO table
I will place the function in the QTDE column, in the (ACOES) VALORES spreadsheet
Here is the EXTRATO table in the EXTRATO spreadsheet. With my current VBA function it is only adding the values of the lines in yellow (4, 11, 9), see that some do not respect the conditions I want, giving me the value of 24 with the result.
Here is my current code:
Function QTDE(acao As String) As Double
Dim ws As Worksheet
Dim tbl As ListObject
Dim cel As Range
Dim tipoCol As ListColumn
Dim ativoCol As ListColumn
Dim qtdeCol As ListColumn
Dim totalQtde As Double
Set ws = ThisWorkbook.Sheets("EXTRATO")
Set tbl = ws.ListObjects("EXTRATO")
Set tipoCol = tbl.ListColumns("TIPO")
Set ativoCol = tbl.ListColumns("ATIVO")
Set qtdeCol = tbl.ListColumns("QTDE")
For Each cel In tipoCol.DataBodyRange
If (cel.Value = "COMPRA" Or cel.Value = "BONIFICACAO" Or cel.Value = "SUBSCRICAO") And ativoCol.DataBodyRange(cel.Row).Value = acao Then
totalQtde = totalQtde + qtdeCol.DataBodyRange.Cells(cel.Row).Value
End If
Next cel
QTDE = totalQtde
End Function