As funções abaixo lhe permitirão carregar (LoadMonths()) em uma Matriz, 12 meses, sendo o primeiro uma data passada e os próximos 11 meses serão calculados retroativamente. Depois poderão recuperar (ReturnMonth()) estas datas.
Global nMeses(12) As Date
Sub LoadMonths()' Author: André Bernardes' Date: 20/08/14 - 09:53' Action: Cria tabelas de Regionais para análise.' Application: Analysis****Regional®' Test line: nMeses (1), nMeses(2), nMeses(3), nMeses(4), nMeses(5), nMeses(6), nMeses(7), nMeses(8), nMeses(9), nMeses(10), nMeses(11), nMeses(12)' Test line: Debug.Print ReturnMonth(1), ReturnMonth(2), ReturnMonth(3), ReturnMonth(4), ReturnMonth(5), ReturnMonth(6), ReturnMonth(7), ReturnMonth(8), ReturnMonth(9), ReturnMonth(10), ReturnMonth(11), ReturnMonth(12)Dim i As IntegerDim Flag As BooleanDim LastDate As DateLet Flag = TrueFor i = 1 To 12If Flag ThenLet nMeses(i) = Sheets("Analise").Range("I5").ValueLet LastDate = nMeses(i)Let Flag = FalseElseLet nMeses(i) = DateAdd("m", -1, LastDate)Let LastDate = nMeses(i)End IfNextEnd Sub
Caso queiram traduzir os meses para outros idiomas basta que alterem os meses.Function ReturnMonth (nMnth As Integer) As String' Author: André Bernardes' Date: 20/08/14 - 09:53' Action: Cria tabelas de Regionais para análise.' Application: AnalysisMDTRRegional®' Test line: nMeses (1), nMeses(2), nMeses(3), nMeses(4), nMeses(5), nMeses(6), nMeses(7), nMeses(8), nMeses(9), nMeses(10), nMeses(11), nMeses(12)' Test line: Debug.Print ReturnMonth(1), ReturnMonth(2), ReturnMonth(3), ReturnMonth(4), ReturnMonth(5), ReturnMonth(6), ReturnMonth(7), ReturnMonth(8), ReturnMonth(9), ReturnMonth(10), ReturnMonth(11), ReturnMonth(12)Dim nMonth As StringDim nYear As StringDim nTitle01 As StringLet nMonth = Mid(Format(nMeses(nMnth), "DD/MM/YYYY"), 4, 2)Let nYear = Year(nMeses(nMnth))Let nTitle01 = UCase(Left(Format(Month(nMonth), "mmm"), 3))If nMonth = "01" ThenLet ReturnMonth = "JAN|" & nYearElseIf nMonth = "02" ThenLet ReturnMonth = "FEV|" & nYearElseIf nMonth = "03" ThenLet ReturnMonth = "MAR|" & nYearElseIf nMonth = "04" ThenLet ReturnMonth = "ABR|" & nYearElseIf nMonth = "05" ThenLet ReturnMonth = "MAI|" & nYearElseIf nMonth = "06" ThenLet ReturnMonth = "JUN|" & nYearElseIf nMonth = "07" ThenLet ReturnMonth = "JUL|" & nYearElseIf nMonth = "08" ThenLet ReturnMonth = "AGO|" & nYearElseIf nMonth = "09" ThenLet ReturnMonth = "SET|" & nYearElseIf nMonth = "10" ThenLet ReturnMonth = "OUT|" & nYearElseIf nMonth = "11" ThenLet ReturnMonth = "NOV|" & nYearElseIf nMonth = "12" ThenLet ReturnMonth = "DEZ|" & nYearEnd IfEnd Function