VBA find value in range, paste values + loop

InputF9repeat

New Member
Joined
Apr 19, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm trying to adapt the VBA code from this thread into a loop. The aim is to copy and paste a range of cells (Name Range "Output") for each scenario that I'm running into a numbered list (Name Range "Paste.Index").

There are 10 scenarios that are numbered 1-10 (Name Range "NumScenarios") and whichever scenario is chosen is shown in the cell (Name Range "ActiveScenario"). My intention is for the macro to cycle through each of the 10 scenarios and then copy and paste values for each scenario based on the location in the list for that scenario. So for example, if scenario 5 is active, then the macro would copy "Output" and then paste the values next to where the number 5 appears in a list. For some reason the code refuses to paste anything past scenario 6 and it pastes over some of the older pasted values for some reason.

Any help would be greatly appreciated.

VBA Code:
Public Sub UpdateScenarios()

Dim intCount As Integer

Application.Calculation = xlCalculationAutomatic

For intCount = 1 To Range("NumScenarios").Count - 1
        Range("ActiveScenario").Value = intCount
        CallCopyPaste
        
Next intCount

End Sub

Sub CopyPaste()

FindItem = Range("ActiveScenario").Value
FoundItem = Range("Paste.Index").Find(What:=FindItem).Address
PasteLocation = Range(FoundItem).Offset(1, 7).Address

Range("Output").Copy
Range(PasteLocation).PasteSpecial xlPasteValues
Application.CutCopyMode = False

End Sub
 

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,214,902
Messages
6,122,161
Members
449,069
Latest member
msilva74

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