VBA代码,运行时错误9下标超出范围

时间:2013-03-17 14:54:02

标签: arrays vba

有人可以帮助我理解为什么我从代码中得到运行时错误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

错误行是星号行。 是因为我不能重复使用prod数组吗?我不明白为什么我得到这个错误。 任何帮助将不胜感激!

0 个答案:

没有答案