Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
  • Avatar do usuário
#33843
Olá, boa tarde galera.

Alguém poderia dar uma força no seguinte cód.:

Preciso realizar uma soma dos 10 último valores a partir de "UltCel"...

Código: Selecionar todos
UltCel = Sheets("Comum").Cells(Sheets("Comum").Rows.Count, 2).End(xlUp).Row

Essa coluna será alimentada por um formulário, porém preciso pegar sempre o totalizador deste intervalo (range).


Muito obrigado!
#33851
Experimente:
Código: Selecionar todos
Sub SomaÚltimasDez()
 Dim UltCel  As Long
  With Sheets("Comum")
   UltCel = .Cells(Rows.Count, 2).End(3).Row
   .Cells(UltCel + 1, 2).Formula = "=SUM(" & Range(Cells(UltCel - 9, 2), Cells(UltCel, 2)).Address & ")"
   '.Cells(UltCel + 1, 2).Value = .Cells(UltCel + 1, 2).Value
  End With
End Sub
#33861
Osvaldo, muito obrigado!

havia conseguido também! Ficou assim:
Código: Selecionar todos
For i = UltCel To UltCel - 9 Step -1
        total = total + CDbl(Range("B" & i).Value)
    Next
Porém, só funciona se tiver 10linhas preenchidas...mas, gostaria que isso fosse manipulado para havendo valor menor ele me traga essa soma..

Tipo uma soma com limite partindo da última célula com preenchimento até o limite de 10 células anteriores à ultima cel preenchida.
#33868
AMORIM123 escreveu: ... só funciona se tiver 10linhas preenchidas...mas, gostaria que isso fosse manipulado para havendo valor menor ele me traga essa soma..
Código: Selecionar todos
Sub SomaÚltimasAtéDez()
 Dim UltCel  As Long
  With Sheets("Comum")
   UltCel = .Cells(Rows.Count, 2).End(3).Row
    If UltCel < 10 Then
     .Cells(UltCel + 1, 2).Formula = "=SUM(" & Range("B1", Cells(UltCel, 2)).Address & ")"
    Else: .Cells(UltCel + 1, 2).Formula = "=SUM(" & Range(Cells(UltCel - 9, 2), Cells(UltCel, 2)).Address & ")"
     '.Cells(UltCel + 1, 2).Value = .Cells(UltCel + 1, 2).Value
    End If
  End With
End Sub

#33889
Deixei assim:
Código: Selecionar todos
With wshComum

    UltCel = .Cells(.Rows.Count, 2).End(xlUp).Row
    'soma as 12 ultimas linhas ou quantidade menor que doze
    For i = UltCel To UltCel - 10 Step -1
        If i > 1 Then
            total = total + CDbl(Range("C" & i).Value)
        Else
            Exit For
        End If
    Next
        
'

End With
Muito Obrigado Osvaldo pelo tempo e atenção!
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord