arraysexcelvbaloopsnested-loops

Looping with Multiple Arrays


i am having trouble creating an efficient code that loops and returns the result for 7 scenarios starting in a particular cell and having each scenario return in the cell immediately below the particular cell.

Thank you for your help and apologies for my noob skill level.

The long-form script I'm running is this:

Sub Macro1()

    Dim X As Worksheet
    Dim Y As Worksheet
    Set X = Sheets("Scenarios")
    Set Y = Sheets("Portfolio Model")
    
    'Run Flat Scenarios
    X.Select
    Range("M2").Select
    If Range("M2") = "N" Then Range("M2").Value = "Y" Else Range("M2").Value = "Y"
                
    '#1 Flat Scenario
    Y.Select
    Range("GO8").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP8").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
                
    '#2 Flat Scenario
    Y.Select
    Range("GO9").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP9").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
                
    '#3 Flat Scenario
    Y.Select
    Range("GO10").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP10").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
                
    '#4 Flat Scenario
    Y.Select
    Range("GO11").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP11").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    
    '#5 Flat Scenario
    Y.Select
    Range("GO12").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP12").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    
    '#6 Flat Scenario
    Y.Select
    Range("GO13").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP13").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    
    '#7 Flat Scenario
    Y.Select
    Range("GO14").Select
        Selection.Copy
    Range("G3").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    Calculate
    Range("GK5").Select
        Selection.Copy
    Range("GP14").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    
End Sub

This is where I am at in terms of making that script more efficient and attempting to run in loops:

Sub Macro2()

    Dim X As Worksheet
    Dim Y As Worksheet
    Set X = Sheets("Scenarios")
    Set Y = Sheets("Portfolio Model")
    
    'Run Flat Scenarios
    X.Select
    Range("M2").Select
    If Range("M2") = "N" Then Range("M2").Value = "Y" Else Range("M2").Value = "Y"
            
    Dim j As Variant
    Dim jArray As Variant
    jArray = Array(0.085, 0.0875, 0.09, 0.0925, 0.095, 0.0975, 0.01)

    Dim i As Variant
    Dim iArray As Variant
    iArray = Array(1, 2, 3, 4, 5, 6, 7)
    
    For Each i In iArray
        Range("GK5").Copy
        Range("GP" & 7 + i).PasteSpecial xlValues
    
        For Each j In jArray
            Range("G3").Value = j
            Calculate
        Next
    Next

End Sub

Solution

  • Improving Macro Recorder Code: Coding Scenarios

    Reading from Cells

    Sub Macro1()
    
        ' Define constants.
        Const SCENARIOS_COUNT As Long = 7
        Const COLUMN_OFFSET As Long = 1
    
        ' Reference the workbook and worksheets.
        
        Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
        ' If it isn't, reference it by its name or use 'ActiveWorkbook'.
    
        Dim sws As Worksheet: Set sws = wb.Sheets("Scenarios")
        Dim pws As Worksheet: Set pws = Sheets("Portfolio Model")
        
        'Run Flat Scenarios
        
        With sws.Range("M2")
            If .Value = "N" Then .Value = "Y" Else .Value = "N" ' !?!
        End With
                    
        With pws
            Dim fcell As Range: Set fcell = .Range("GO8")
            Dim RowOffset As Long
            For RowOffset = 0 To SCENARIOS_COUNT - 1
                .Range("G3").Value = fcell.Offset(RowOffset).Value
                .Calculate
                fcell.Offset(RowOffset, COLUMN_OFFSET).Value = .Range("GK5").Value
            Next RowOffset
        End With
        
    End Sub
    

    Reading from Array

    Sub Macro2()
    
        ' Define constants.
        Dim VALUES() As Variant: VALUES = VBA.Array( _
            0.085, 0.0875, 0.09, 0.0925, 0.095, 0.0975, 0.01)
    
        ' Reference the workbook and worksheets.
        
        Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
        ' If it isn't, reference it by its name or use 'ActiveWorkbook'.
    
        Dim sws As Worksheet: Set sws = wb.Sheets("Scenarios")
        Dim pws As Worksheet: Set pws = Sheets("Portfolio Model")
        
        'Run Flat Scenarios
        
        With sws.Range("M2")
            If .Value = "N" Then .Value = "Y" Else .Value = "N" ' !?!
        End With
                    
        With pws
            Dim fcell As Range: Set fcell = .Range("GP8")
            Dim Index As Long
            For Index = 0 To UBound(VALUES)
                .Range("G3").Value = VALUES(Index)
                .Calculate
                fcell.Offset(Index).Value = .Range("GK5").Value
            Next Index
        End With
        
    End Sub