【发布时间】:2015-11-09 22:56:15
【问题描述】:
我试图让用户根据托盘数量输入重量。 NumberPallets 是通过代码中其他位置的输入框设置的。
NumberPallets 是 3。我希望它循环 3 次并询问每个托盘的重量并将其存储到 PalletWeights(p) 中,因此它看起来像这样:
PalletWeight(1) = 200
PalletWeight(2) = 100
PalletWeight(3) = 300
TotalPalletWeight = 600
现在它给了我一个下标错误,我相信这是因为我没有正确地做数组。我用谷歌搜索过尝试使用 PalletWeight(Ubound(PalletWeight)) 但这也不起作用。其他 Google 搜索几乎不会产生单独获取 InputBox 数据而不是逗号分隔列表的结果。
我需要改变什么来实现这个功能?
ReDim PalletWeights(1 to NumberPallets) 'Added based on an answer on this question
Dim PalletWeights() As String 'Array of pallet weights
Dim p As Integer
p = 1
Do While p <= NumberPallets
PalletWeights(p) = Application.InputBox(Prompt:="What is the weight of pallet number " & p & "?", Type:=1)
p = p + 1
Loop
TotalPalletWeight = Application.WorksheetFunction.Sum(PalletWeights)
新的完整工作代码:
'Pallet Weights
Dim PalletWeights() 'Array of pallet weights
Dim p As Integer
p = 1
ReDim PalletWeights(1 To NumberPallets)
Do While p <= NumberPallets
'Total Weight
PalletWeights(p) = Application.InputBox(Prompt:="What is the weight of pallet number " & p & "?", Type:=1)
TotalWeight = TotalWeight + PalletWeights(p)
p = p + 1
Loop
【问题讨论】: