VBA to match date and account then paste on another sheet

excelbeg

New Member
Joined
May 16, 2017
Messages
4
Hi,

I created a macro for a budget that counts the number of matching months between a sheet in the budget "Budget" and another sheet "Trailing12". I then created 12 separate macros that paste an index match match formula into each column's range based on the number of matching months (i.e. if two months match, the macro pastes the specific formula into column I and column J, if three months match, macro pastes formula into column I, J, and K, etc. until column T).

My questions are: 1.) Is there a way to make this code work, so that if I insert additional rows (accounts), I won't have to update the range? 2.) Is there a way to make the macro run faster? I presume all the iterations and copy/paste are slowing it down substantially.

Thank you in advance.

Code:
Sub PasteActualsI()
    ' This macro will pull historical data by account from Trailing12 tab and paste in Column I
    Sheets("Budget").Select
    Range("I12:I18, I22:I28, I34:I41, I45:I68, I74:I88, I92:I100, I104:I129, I133:I145, I149:I165, I169:I185, I189:I197, I201:I220, I224, I228, I232:I234, I242:I245, I249:I277").Select
    Range("I22").Activate
    Range("I34").Activate
    Range("I45").Activate
    Range("I74").Activate
    Range("I92").Activate
    Range("I104").Activate
    Range("I133").Activate
    Range("I149").Activate
    Range("I169").Activate
    Range("I189").Activate
    Range("I201").Activate
    Range("I224").Activate
    Range("I228").Activate
    Range("I232").Activate
    Range("I242").Activate
    Range("I249").Activate
    Selection.Formula = "=IFNA(INDEX(Trailing12!$A$9:$N$<wbr>350,MATCH('Budget'!A12,Trailing12!$A$9:$A$<wbr>350,0),MATCH($I$10,Trailing12!$A$<wbr>9:$N$9,0)),0)"
    Range("I12:I18, I22:I28, I34:I41, I45:I68, I74:I88, I92:I100, I104:I129, I133:I145, I149:I165, I169:I185, I189:I197, I201:I220, I224, I228, I232:I234, I242:I245, I249:I277").Font.Color = vbRed
End Sub



Code:
Sub main_macro()
    If Range("C5").Value = 12 Then
        Call Month_12
    ElseIf Range("C5").Value = 11 Then
        Call Month_11
    ElseIf Range("C5").Value = 10 Then
        Call Month_10
    ElseIf Range("C5").Value = 9 Then
        Call Month_9
    ElseIf Range("C5").Value = 8 Then
        Call Month_8
    ElseIf Range("C5").Value = 7 Then
        Call Month_7
    ElseIf Range("C5").Value = 6 Then
        Call Month_6
    ElseIf Range("C5").Value = 5 Then
        Call Month_5
    ElseIf Range("C5").Value = 4 Then
        Call Month_4
    ElseIf Range("C5").Value = 3 Then
        Call Month_3
    ElseIf Range("C5").Value = 2 Then
        Call Month_2
    ElseIf Range("C5").Value = 1 Then
        Call Month_1
    ElseIf Range("C5").Value = 0 Then
        MsgBox ("Dates do not match on budget template and Trailing12 tab. Please adjust dates")
    Else
        Return
    End If
End Sub


Code:
Sub Month_12()
PasteActualsI
PasteActualsJ
PasteActualsK
PasteActualsL
PasteActualsM
PasteActualsN
PasteActualsO
PasteActualsP
PasteActualsQ
PasteActualsR
PasteActualsS
PasteActualsT
End Sub


Code:
Sub Month_11()
PasteActualsI
PasteActualsJ
PasteActualsK
PasteActualsL
PasteActualsM
PasteActualsN
PasteActualsO
PasteActualsP
PasteActualsQ
PasteActualsR
PasteActualsS
End Sub

etc. all the way to Sub Month_1
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top