Simple VBA Copy & Paste Loop

Stuaart

New Member
Joined
Feb 16, 2018
Messages
3
Hi there MrExcel community, this is my first forum post! So I have just recently started a new job and blagged that I could do some basic VBA and ...well... let's just say that I am on a steep learning curve :)

I have a basic repetitive task that I am hoping one of you could help me with. I've spent around 30 mins trying it, however, I am still getting to grips with the basic VBA language, operators, expressions and so on. What I am trying to do is this:


On sheet "sensitivity analysis" select Cell A9 and paste the value into A3
Select Cells I3:K3 and paste into Cells I9:K9

Then repeat for line 10 as follows:=

On sheet "sensitivity analysis" select Cell A10 and paste the value into A3
Select Cells I3:K3 and paste into Cells I10:K10

The repeat for lines 11 onward until the cell is blank.

This is effectively taking a scenario ID in column A and entering it into cell A3. This triggers cells B2 to D2 to lookup the values from the appropriate row which serve as inputs for a scenario in a model. The outputs of the scenario are calculated (from other sheets so Data Table cannot be used) into cells I:K.

I'd like to run 1,000 scenarios.

I hope this makes sense, any questions please let me know. Your help is much appreciated.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Stuart,

Remember to try macros on a copy of your workbook first.
Please give the following a go. I believe it is what you are after.

code Error, revisiting now
 
Last edited:
Upvote 0
Apologies,

have now got the code working

Code:
Option Explicit

Sub SaveStuart()
Dim i As Long


On Error GoTo ErrHandle
Application.ScreenUpdating = False


Sheets("sensitivity analysis").Select
For i = 9 To 1009
Cells(3, 1).Value = Cells(i, 1).Value
Range(Cells(i, 9), Cells(i, 11)).Value = Range(Cells(3, 9), Cells(3, 11)).Value
Next i


Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub


ErrHandle:
Application.ScreenUpdating = True
MsgBox "The code has encountered an error", vbOKOnly + vbCritical, "Error"
Exit Sub


End Sub
 
Upvote 0
Try this code
Code:
Sub Macro3()Dim LR As Long


With Sheets("sensitivity analysis")
LR = Range("A9").End(xlDown).Row
Range("A3") = Range("A" & LR)
Range("I" & LR & ":K" & LR) = Range("I3:K3")
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,265
Members
449,219
Latest member
daynle

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