SOMAMASES NO COM CODIGO VBA

leonardo092004

11-03-2020 13:15:38

Bom dia preciso de um ajuda,
tentei de muitas maneiras e não consegui fazer o SOMASES pelo VBA com informações de uma planilha com a outra

O que eu preciso, era que soma-se total da base de calculo de acordo com os numero das fiscais , do CST e do CFOP da aba C170 na coluna base de da aba C190..

Segue anexo uma planilha teste ..

Desde ja agradeço!!

Reinaldo

11-03-2020 18:04:49

Uma possibilidade

Código: Selecionar todos

Sub SomaNF()
Dim x As Integer, y As Integer
Dim uLinC190 As Integer, uLinC170 As Integer

uLinC170 = Sheets("C170").Cells(Rows.Count, "C").End(xlUp).Row
uLinC190 = Sheets("C190").Cells(Rows.Count, "C").End(xlUp).Row
    For x = 3 To uLinC170
        For y = 3 To uLinC190
            If Sheets("C190").Cells(y, 3).Value = Sheets("C170").Cells(x, 3).Value Then
                Sheets("C190").Cells(y, 6).Value = Sheets("C190").Cells(y, 6).Value + Sheets("C170").Cells(x, 6).Value
                Sheets("C190").Cells(y, 7).Value = Sheets("C190").Cells(y, 7).Value + Sheets("C170").Cells(x, 7).Value
            End If
        Next
    Next
End Sub
Post Duplicado?? https://www.tomasvasquez.com.br/forum/v ... =21&t=7487

leonardo092004

11-03-2020 20:37:33

Obrigado pelo retorno..

o que acontece é que a planilha onde vou inserir esse condigo ela importar arquivo sped..
então nem sempre a planilha1 ou 2 ou 3 vai levar o nome de C190 ou seja elas variam.. e esse código precisa executar somente na planilha C190..

tentei usar dessa forma, mas não funcionou ..

Sub Somar()

Dim linha As Long
Dim linha2 As Long


linha = 2
linha2 = 2

Dim base As Double
Dim icms As Double


While Worksheets("C190").Cells("A" & linha).Value = ""
linha2 = 2
base = 0
icms = 0
While Sheets("C170").Cells("B" & linha2).Value <> ""
If Sheets("C170").Cells("C" & linha2).Value = Sheets("C190").Cells("C" & linha).Value Then
If Sheets("C170").Cells("D" & linha2).Value = Sheets("C190").Cells("D" & linha).Value Then
If Sheets("C170").Cells("E" & linha2).Value = Sheets("C190").Cells("E" & linha).Value Then

base = base + Sheets("C170").Cells("F" & linha2).Value
icms = icms + Sheets("C170").Cells("G" & linha2).Value
Sheets("C190").Cells("F" & linha).Value = base
Sheets("C190").Cells("G" & linha).Value = icms


End If

End If

End If

linha2 = linha2 + 1
Wend

linha = linha + 1

Wend



End Sub