VBA:error 9,下标越界。

拉二 2013-05-12 01:37:15
在执行下面的代码的时候,出现error 9,下标越界的错误,错误出现在这一句:
arrA(j) = arrA(j) + arr(i, j) + 8

求大侠们帮忙看看怎么去解决这个问题呢?有时候还提示这一句的类型不匹配什么的。。。。。


Sub calculate()
Dim i As Integer, j As Integer, a As Integer, b As Integer
Dim arrA(1 To 40) As Double, arrB(1 To 40) As Double, arr, arr1, arr2, arr3
Dim d1 As New dictionary, d2 As New dictionary
Dim d3 As New dictionary
Dim d4 As Variant
Dim StartT As Variant, endT As Variant


'For i = 1 To Range("A65536").End(xlUp).Row
'arr(i) = Range("A" & i).Value
'Cells(i, 2) = arr(i)
'Next
'i = i + 1
Application.DisplayAlerts = False
Application.ScreenUpdating = False
arr = Sheets("Sheet1").Range("A2:H" & Sheets("Sheet1").Range("A65536").End(xlUp).Row)
arr1 = Sheets("Sheet1").Range("I2:J" & Sheets("Sheet1").Range("I2").End(xlDown).Row)
arr2 = Sheets("Sheet1").Range("B2:H33" & Sheets("Sheet1").Range("B2").End(xlDown).Row)

Set d1 = New dictionary
Set d2 = New dictionary
Set d3 = New dictionary
'ActiveSheet.Range("A1:H" & Sheets("Sheet1").Range("A65536").End(xlUp).Row).Select
For i = 2 To UBound(arr) 'get the value of all cells to arr
If Not d1.Exists(arr(i, 2)) Then
d1(arr(i, 2)) = i
End If
Next
For i = 2 To UBound(arr1) 'get the value of the date in A column to arr
If Not d2.Exists(arr1(i, 2)) Then
d2(arr1(i, 2)) = i
End If
Next
'For i = 2 To UBound(arr2) 'get the number of the value
'For j = 2 To UBound(arr2)
' If Not d3.Exists(arr2(i, 2)) Then
' d3(arr2(i, 2)) = [B2:H33]
' End If
'Next
'Next
StartT = Cells(17, 9)
endT = Cells(17, 10)
For j = 3 To UBound(arr, 2)
For i = d1(StartT) To d1(endT)
If Not d2.Exists(arr(i, 2)) Then
arrA(j) = arrA(j) + arr(i, j) + 8

arrB(j) = arrB(j) + arr(i, j)
Else
arrA(j) = arrA(j) + arr(i, j)
arrB(j) = arrB(j) + arr(i, j)
End If
Next
Next
Cells(15, 12) = Application.Max(arrB())
Cells(15, 15) = Application.Sum(arrA())

Set arr = Nothing
Set arr1 = Nothing

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
...全文
200 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsd999 2013-05-13
  • 打赏
  • 举报
回复
先看看arr的数组有多大,下标越界后看看i和j的值。
MMICE 2013-05-12
  • 打赏
  • 举报
回复
看下你工作表的名字和代码里的是不是一致的

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧