Loop validation, copy output from same range and paste in different ranges

cyenwong

New Member
Joined
Feb 2, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I'm trying to loop through 10 scenarios via a validation list. On each scenario the calculation engine provides an output in a fixed range. The output needs to be copied / pasted onto a dashboard sheet going down the row with fixed distance (so the dashboard sheet will have 10 sets of output).

The validation list, output and dashboard are in 3 different sheets

I tried the following VBA code, however on each run the output seems to be very different. I tried F8, the validation list looping is working, output is also changing correctly. But the pasting bit seems to differ every time I run, although some random stuff that gets copied are being pasted into the correct range.

Appreciate if someone can tell me what's wrong, thanks!

VBA Code:
Sub LoopThroughScenarioList_And_PasteScenarioOutput()

Dim rng As Range
Dim dataValidationArray As Variant
Dim i As Integer 'used for # of scenario
Dim j As Integer 'used for # of output pasting
Dim rows As Integer

'Set the cell which contains the Scenario validation list 
Set rng = Sheets("LC model").Range("AC8") ':AJ8

On Error Resume Next 'in case there is error

'Create an array from our Data Validation formula so it knows the boundary of the loop
rows = Range(Replace(rng.Validation.Formula1, "=", "")).rows.Count
ReDim dataValidationArray(1 To rows)

For i = 1 To rows
    dataValidationArray(i) = _
        Range(Replace(rng.Validation.Formula1, "=", "")).Cells(i, 1)
Next i

'Loop through all the scenarios in array defined above
For i = LBound(dataValidationArray) To UBound(dataValidationArray)

    'Change the value in the scenario selection cell
    rng.Value = dataValidationArray(i)
    
    'Force the sheet to recalculate
    Application.Calculate
    
    'Copy the output
    Sheets("Macro to copy").Range("G6:H8").Select
    Selection.Copy
    
    'Paste the output, starting from Scenario 1 location
    'Sheets("Macro to copy").Range("M6").Offset(j, 0).Select 'j = 0 right now
    Sheets("Baseline & Scenario Output").Select
    Range("Q24").Offset(j, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
            
    'Update value j so next paste is on Scenario 2 location. 16 is the distance between each scenario output
    j = j + 16
      
Next i

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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