我需要将for循环中的值存储到数组中

时间:2014-04-12 13:15:27

标签: arrays vba

基本上我需要提示用户提供与三年第一季度的阳光总量相关的值。


我的主表单上有一个显示“开始”的按钮。

单击此按钮时,我想要一个提示框打开。此提示框应提示用户输入在三年内拆分的9个值。 E.g

"Please enter the total sunshine for January 2011"
"Please enter the total sunshine for February 2011"
"Please enter the total sunshine for March 2011"

"Please enter the total sunshine for January 2012"
"Please enter the total sunshine for February 2012"
"Please enter the total sunshine for March 2012"

"Please enter the total sunshine for January 2013"
"Please enter the total sunshine for February 2013"
"Please enter the total sunshine for March 2013"

我需要将值存储在单独的数组中。因此,与三年中每一年相对应的输入值应包含在它们自己的数组中。

1 个答案:

答案 0 :(得分:0)

以下代码将提供您想要的解决方案。由于我不知道你输入后会对这些值做些什么,所以我建议你:(a)将它们保存在桌子或其他东西上; (b)添加代码以处理非数字值(即重试)。如果我是一名用户,每次运行例程时我都要回答9个问题,我就不会很开心......

Sub Get_Sunshine_Values()
Dim a2011(3) As Double
Dim a2012(3) As Double
Dim a2013(3) As Double

a2011(1) = InputBox("Please enter the total sunshine for January 2011", "Sunshine")
a2011(2) = InputBox("Please enter the total sunshine for February 2011", "Sunshine")
a2011(3) = InputBox("Please enter the total sunshine for March 2011", "Sunshine")

a2012(1) = InputBox("Please enter the total sunshine for January 2012", "Sunshine")
' repeat for Feb & Mar.

a2013(1) = InputBox("Please enter the total sunshine for January 2013", "Sunshine")
' repeat for Feb & Mar.

End Sub