简体   繁体   中英

Assign Value of Loop Counter to Cell

I'm trying to run a scenario analysis in Excel:

I have scenarios 1 to XX in the workbook, which then assigns values to fields based on the scenario selected.

I'm trying to assign the variable i to the cell "ScenarioSwitch" via loop.

Sub Sensitivity()

Dim i As Integer
Dim MaxScenario As Long

MaxScenario = ThisWorkbook.Sheets("Income").Range("E76", Range("E76").End(xlDown)).Count

For i = 1 To MaxScenario

    Range("ScenarioSwitch").Value = i

Next i

End Sub

You should use Long not Integer for declaring for loop variables. If you need to loop through Column E starting at row 76 then you can just begin your for loop at 76.

Sub Sensitivity()

Dim i As Long
Dim MaxScenario As Long

MaxScenario = ThisWorkbook.Sheets("Income").Range("E" & Rows.Count).End(xlUp).Row

For i = 76 To MaxScenario

Range("A" & i).Value = i

Next i

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM