1

有人可以帮我理解为什么我的代码会出现运行时错误 9(下标超出范围)吗?

  Dim prod(1 To 6) As String

  prod(1) = "001"
  prod(2) = "002"
  prod(3) = "003"
  prod(4) = "004"
  prod(5) = "005"
  prod(6) = "006"

  Dim sum(1 To 6) As Double
  For i = 1 to 6 Step 1
    sum(i) = 0
  Next i

  Dim sumA(1 To 12) As Double
  For i = 1 To 12 Step 1
    sumA(i) = 0
  Next i

  Sheets("Punching").Activate
  LR = Cells(Rows.Count, "G").End(xlUp).Row

  For i = 4 To LR Step 1
    For j = 1 To UBound(prod) Step 1
      If Cells(i, 11) = prod(j) Then
        sum(j) = sum(j) + Cells(i, 18).Value
      Else
        sum(j) = sum(j)
      End If
    Next j
  Next i

  For k = 4 To LR Step 1
    For l = 1 To UBound(sumA) Step 2
      **If Cells(k, 11) = prod(l) Then**
        sumA(l) = sumA(l) + Cells(k, 19).Value
        sumA(l + 1) = sumA(l + 1) + Cells(k, 20).Value
      Else
        sum(l) = sum(l)
      End If
    Next l
  Next k

错误线是星号线。是因为我不能重用产品数组吗?我不明白为什么我会收到此错误。任何帮助将不胜感激!

4

0 回答 0