Macro to copy/paste input, run calculation and the copy/paste outputs

RccScln

New Member
Joined
Jul 27, 2016
Messages
27
Hello all
I am a novice in the use of excel macros. I would need help to automate some easy actions such as substituting within a financial model calculation certain inputs systematically - let's say n scenarios (input scenario 1, in put scenario 2 input scenario3 n=number of scenarios) and have the model calculate them and copy and paste the calculated outputs into a recap table.
I attache below a minisheet hoping is helpful.
Thank you in advance

Book1
ABCDEFGHIJKLMN
1
2Input model working
315.2
45%Reacp table
512%
65InputSCENARIO 1SCENARIO 2SCENARIO 3
7AAA15.210255
831/12/202401/01/202502/01/202603/01/2027BBB5%4%3%6%
9NCF-800300200550CCC10%11%15%22%
10DDD5431
11Outptus generated with calcl
12IRR27%
13MOIC1.3OutputSCENARIO 1SCENARIO 2SCENARIO 3
14WAL5IRR27%0%15%0%
15PP116WAL5.05.05.05.0
16MOIC5.00.00.00.0
17PP
18
Sheet1
Cell Formulas
RangeFormula
B12B12=+XIRR(C9:F9,C8:F8)
B13B13=-SUM(D9:F9)/C9
J14,L16:N16,J16,N14,L14J14=+B12
B15B15=+XNPV(B5,C9:F9,C8:F8)
 

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.
It is not at all clear as to which cells you want copied and pasted either before or after the calculation so I made some guesses but this code show you how to do it:
VBA Code:
Sub test()
For i = 1 To 3
  inarr = Range(Cells(7, 11 + i), Cells(10, 11 + i))   ' pick L7:L10, M7:M10, and N7:N10 in turn
  Range("j7:j10") = inarr    ' paste into J7:J10
  Application.Calculate
  outarr = Range("j14:j16") ' pick up the output
  Range(Cells(14, 11 + i), Cells(16, 11 + i)) = outarr ' Paste into  L14:L16, M14:M16, and N14:N16 in turn
 Next i

End Sub
 
Upvote 0
Sorry offthelip, I will try to make it clearer. I want to copy cells L7:L10 to B3:B6, run calculation that will affect outputs in B12:B15, then copy these outputs in recap table L13:L16; then again I want to copy cells M7:M10 to B3:B6, run calculation that will affect outputs in B12:B15, then copy these outputs in recap table M13:M16. And then again I hope it clear.
But I think your macro should be able to work for my purposte.
Last question: to write a macro what should I do? View / Macros / Record and then copy paste your code is the best?

Last last question: how can i support you with a coffee?
Thanks
 
Upvote 0
I have altered the code to do the copying just as you describe:
VBA Code:
Sub test()
For i = 1 To 3
  inarr = Range(Cells(7, 11 + i), Cells(10, 11 + i))   ' pick L7:L10, M7:M10, and N7:N10 in turn
  Range("B3:B6") = inarr    ' paste into B3:b6
  Application.Calculate
  outarr = Range("B12:B15") ' pick up the output
  Range(Cells(13, 11 + i), Cells(16, 11 + i)) = outarr ' Paste into  L13:L16, M13:M16, and N13:N16 in turn
 Next i

End Sub

I am not the best person to advise on how to start with macros since I know how I do it but there are lots of ways.
I always have the developer tab in view, which you get from Excel settings, customise ribbon , then on the developer tab select visual basic and in the window that appears select the workbook, right click and insert module, and then paste the code in the module. However this may not be the easiest way of doing this.
 
Upvote 0
Thank you very much. What if the recap table and input are in another sheet different from the out where the working model input works? Shall I add the sheet number in this portion of code?

Range("B3:B6") = inarr ' paste into B3:b6

Thanks
 
Upvote 0
Yes you can the best way to do it is using the "with" statement like this:
VBA Code:
Sub test()
For i = 1 To 3
With Worksheets("Sheet2")
  inarr = .Range(.Cells(7, 11 + i), .Cells(10, 11 + i))   ' pick L7:L10, M7:M10, and N7:N10 in turn
  .Range("B3:B6") = inarr    ' paste into B3:b6
End With
  Application.Calculate
  outarr = Range("B12:B15") ' pick up the output
  Range(Cells(13, 11 + i), Cells(16, 11 + i)) = outarr ' Paste into  L13:L16, M13:M16, and N13:N16 in turn
 Next i

End Sub
this avoids swapping the active cheet to and from the input and the outputs sheets. You can use the with statement construct for both which probably the best way because then the macro will run corrctly regardless of which sheet is active.
Note the addition of the dots in the addressing this is very important
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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