0

我有两个 VBA 程序,一个构建一个数组,然后将其传递给另一个。但是,当第一个程序运行时,会弹出一个 Excel 错误窗口,显示“需要对象”。这是我的代码。我似乎找不到导致此错误的原因。

Sub DetermineLoad()

Worksheets("Gear").Activate

Dim row_src As String
Dim strt_row As String
Dim col_storage As String
Dim Arr() As String
Dim aLength As Integer

row_src = "C"
strt_row = "9"
col_storage = "B"

Range(row_src + strt_row).Activate

While Not IsEmpty(Range(col_storage + Active.Row))
    If Not IsEmpty(ActiveCell) Then
        If ActiveCell.Value Then
            aLength = UBound(Arr) + 1
            ReDim Arr(0 To aLength)
            Arr(aLength) = String(ActiveCell.Column, ActiveCell.Row)
        End If
    End If
    ActiveCell.Offset(0, 1).Activate
Wend

DetermineWeight Arr

End Sub
Sub DetermineWeight(ParamArray Arr() As Variant)

Worksheets("Gear").Activate

Dim weight_lb_trgtCell As String
Dim weight_oz_trgtCell As String
Dim volume_cuin_trgtCell As String
Dim volume_liter_trgtCell As String
Dim col_oz As String
Dim col_lb As String
Dim weight_lb As Double
Dim weight_oz As Double
Dim oz_to_pound As Integer
Dim row_src As String
Dim src_range As Range

weight_lb_trgtCell = "H4"
weight_oz_trgtCell = "I4"

col_oz = "H"
col_lb = "I"
weight_lb = 0       ' 0 out `weight_lb`
weight_oz = 0       ' 0 out `weight_oz`
oz_to_pound = 16    ' 16 ounces to 1 pound

'get sum of weights (pounds, ounces)
For n = LBound(Arr) To UBound(Arr)

    src_range = Range(Arr(n))

    src_row = src_range.Row
    weight_oz = weight_oz + Range(col_oz + src_row).Value
    weight_lb = weight_lb + Range(col_lb + src_row).Value

Next n

'convert pounds
weight_lb = weight_lb + Int(weight_oz / oz_to_pound)
weight_oz = weight_oz - ((weight_oz / oz_to_pound) * oz_to_pound)

Range(weight_lb_trgtCell) = weight_lb
Range(weight_oz_trgtCell) = weight_oz

End Sub
4

2 回答 2

1

为什么你需要 VBA?将此公式放入单元格 H4(剩余盎司):

=MOD(SUMIF(C9:C1000,"<>",H9:H1000),16)

单元格 I4 中的这个公式(总磅数):

=SUMIF(C9:C1000,"<>",I9:I1000)+INT(SUMIF(C9:C1000,"<>",H9:H1000)/16)

调整范围以适应。如果您需要它在新行进入或删除时拾取它们,请使用动态命名 range

于 2013-09-03T19:41:13.270 回答
0

传递数组的另一种方法是在 Module 处设置数组范围,然后使用动态命名范围(如建议的那样),然后 redim 将是动态命名范围的行

于 2013-09-03T21:13:02.110 回答