[英]Assign Value of Loop Counter to Cell
I'm trying to run a scenario analysis in Excel:我正在尝试在 Excel 中运行场景分析:
I have scenarios 1 to XX in the workbook, which then assigns values to fields based on the scenario selected.我在工作簿中有场景 1 到 XX,然后根据所选场景为字段分配值。
I'm trying to assign the variable i to the cell "ScenarioSwitch" via loop.我正在尝试通过循环将变量 i 分配给单元格“ScenarioSwitch”。
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.您应该使用
Long
而不是Integer
来声明 for 循环变量。 If you need to loop through Column E starting at row 76 then you can just begin your for loop at 76.如果您需要从第 76 行开始遍历 E 列,那么您可以从第 76 行开始 for 循环。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.