Planilha Excel VBA cálculos media últimos 12 meses Este Macro do Aplicativo Microsoft Excel VBA(Visual Basic Application), CALCULA A MÉDIA ULTIMOS 12 MESES (02 Planilhas exemplos), excel vba usando um evento Change ao digitar chama macro para calcula a média dois ultimos 12 meses de forma simples, no outro exemplo usei uma checkbox para calculo manual e automatico. ‘//===============’ é o mesmo calculo usadado nas duas planilhas:
Sub somar_ultimos_12_meses()
Dim tSoma, xSoma, tMes As Long
x = Cells(Rows.Count, “c”).End(xlUp).Row + 1
For tMes = x To 1 Step -1
If tMes > 12 Then
tSoma = tSoma + Cells(tMes, “c”).Value
Else
Cells(3, “d”).Value = Round((tSoma / tMes), 0)
For y = 5 To Cells(Rows.Count, “c”).End(xlUp).Row
xSoma = xSoma + Cells(y, “c”)
Next y
Cells(3, “c”).Value = Round(xSoma / (y – 5), 0)
Exit Sub
End If
Next tMes
End Sub
‘//===========’
outra forma de achar a média é suar a Função de Planilha Average(Média)
que retorna a média sem a necessidade do uso de fórmulas
Sub wkf_achar_media()
regiao = Range(“c5” & “:c” & Rows.Count)
‘Cells(1, “h”) = Application.WorksheetFunction.Average(Range(“c5” & “:c” & Rows.Count))
Cells(1, “j”) = Application.WorksheetFunction.Average(regiao)
End Sub
‘//=====’ evento change chamando o macro acima.
Private Sub Worksheet_Change(ByVal Target As Range)
x = Cells(Rows.Count, “c”).End(xlUp).Row + 1
If Not Intersect(Target, Range(“c5” & “:” & “c” & x)) Is Nothing Then
Application.EnableEvents = False
sbx_somar_ultimos_12_meses ‘chamar o macro para calcular
Application.EnableEvents = True
End If
End Sub
‘//====================’
calculando mas usando o CheckBox para desabilitar
Sub sbx_somar_ultimos_12_meses()
Dim tSoma, xSoma, tMes As Long
X = Cells(Rows.Count, “c”).End(xlUp).Row + 1
For tMes = X To 1 Step -1
If tMes > 12 Then
tSoma = tSoma + Cells(tMes, “c”).Value
Else
Cells(3, “d”).Value = Round((tSoma / tMes), 0)
For Y = 5 To Cells(Rows.Count, “c”).End(xlUp).Row
xSoma = xSoma + Cells(Y, “c”)
Next Y
Cells(3, “c”).Value = Round(xSoma / (Y – 5), 0)
Exit Sub
End If
Next tMes
End Sub
‘//============’
Sub wkf_achar_media()
regiao = Range(“c5” & “:c” & Rows.Count)
‘Cells(1, “h”) = Application.WorksheetFunction.Average(Range(“c5” & “:c” & Rows.Count))
Cells(1, “j”) = Application.WorksheetFunction.Average(regiao)
End Sub
‘//=========’
Sub sbx_limpar_teste() ‘não vai funcionar porque o macro esta ligado ao
[c3:d3].ClearContents
End Sub
‘//==========’
‘habilitar eventos caso desabilite-os acidentalmente
Sub sbx_habilitar_eventos()
Application.EnableEvents = True
End Sub
‘//=========’
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
CheckBox1.Caption = “Calculos Automaticos”
sbx_somar_ultimos_12_meses
Else
CheckBox1.Caption = “Calculo Manual”
sbx_somar_ultimos_12_meses
End If
End Sub
‘//=== segundo exemplo
Private Sub Worksheet_Change(ByVal Target As Range)
x = Cells(Rows.Count, “c”).End(xlUp).Row + 1
If CheckBox1.Value = True Then
If Not Intersect(Target, Range(“c5” & “:” & “c” & X)) Is Nothing Then
Application.EnableEvents = False
sbx_somar_ultimos_12_meses
Application.EnableEvents = True
End If
Else
Exit Sub
End If
End Sub
![]()
Aprenda tudo sobre planilhas do Aplicativo Microsoft Excel VBA(Visual Basic Application), sozinho, com baixo custo, praticando com os produtos didáticos
Escola Saberexcel VBA Estudos® – Treinamentos com Macros, Fórmulas e Funções.

PROMOÇÃO ESPECIAL – EXCEL VBA Adquirir Todo Material Didático Escola SaberExcel VBA Estudos
Baixe o exemplo de planilha contendo os macros acima:
Planilha Exccel VBA calculos media ultimos 12 meses

