【问题标题】:Dynamic array that populates via looped inputbox通过循环输入框填充的动态数组
【发布时间】: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

【问题讨论】:

    标签: arrays vba excel


    【解决方案1】:

    您已经声明了一个动态数组。在您明确调整大小之前,它不会调整大小。

    尝试在 Do 循环之前添加

    ReDim PalletWeights(1 to NumberPallets)
    

    【讨论】:

    • 这行得通,但总和不起作用。我输出数组的值,因为它们进入并且值在那里。我也尝试过 TotalWeight=TotalWeight+PalletWeights(p) 但它没有做数学运算。它只需要 100 和 200 就可以得到 TotalWeight 100200。
    • 我明白了。我在数组上取出“作为字符串”,然后将其保留为“Dim PalletWeights()”,这样就修复了总和部分。我只是在它运行时在循环中求和。
    【解决方案2】:

    只需在循环后添加 ReDim 表达式即可。见下文。

    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
    
    ReDim PalletWeights(p)
    
    TotalPalletWeight = Application.WorksheetFunction.Sum(PalletWeights)
    

    【讨论】:

    • 看下面的答案,它已经过测试了。
    • 对我不起作用。我在我的问题中发布了工作代码。
    【解决方案3】:

    首先您需要使用 ReDim 分配数组大小,然后您可以将数据存储在数组中。当您对数据求和时,这就是为什么您需要将数组声明为整数而不是字符串。

        Public Sub ArraySum()
                Dim PalletWeights() As Integer 'Array of pallet weights
                Dim p, TotalPalletWeight As Integer
                ReDim PalletWeights(5)
                p = 1
                Do While p <= 5
                PalletWeights(p) = Application.InputBox(Prompt:="What is the weight of         pallet number " & p & "?", Type:=1)
                 p = p + 1
                Loop
                TotalPalletWeight = Application.WorksheetFunction.Sum(PalletWeights)
                MsgBox TotalPalletWeight
        End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 2020-05-25
      • 2023-03-23
      • 2015-03-01
      • 2020-06-07
      • 2022-07-06
      • 2021-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多