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

时间:2019-01-08 11:52:56

标签: excel vba

我必须每三十秒从sheet6的动态单元格A1中填充一个阵列storageata60。每隔30分钟,此数组将重置为null。如果最大和最小值之差大于7,则消息框将抛出消息。我为此活动分配了4个子项。 iam出现运行时错误9下标超出范围。它在my_procedure的第一行显示错误。代码下方:

Public RunWhen As Double    
Public Const cRunWhat = "my_Procedure"  
Dim I As Integer, n50max As Double, n50min As Double, Max_Min As Double, storedata60() As Double    

Option Explicit 

Sub StartTimer()    
    RunWhen = Now + TimeSerial(0, 0, 30)    
    Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _    
      schedule:=True    
End Sub 


Sub StopTimer() 
   On Error Resume Next 
   Application.OnTime earliesttime:=RunWhen, _  
       procedure:=cRunWhat, schedule:=False 
End Sub 


Sub my_Procedure()  
    storedata60(I) = ThisWorkbook.Sheets("Sheet6").Range("A1").Value    
    n50max = Application.WorksheetFunction.Max(storedata60) 
    n50min = Application.WorksheetFunction.Min(storedata60) 
    Max_Min = n50max - n50min   
    If Max_Min >= 7 Then MsgBox Max_Min 
    I = I + 1   
    ReDim storedata60(I)    
    If I = 60 Then Call reset_zero  
    If I <> 60 Then Call StartTimer 
End Sub 


Sub reset_zero()    
    I = 0   
    Call StartTimer 
End Sub 

1 个答案:

答案 0 :(得分:0)

您必须调整存储阵列60()的大小:

dim storedata60("size you need for this array") as double

编辑:

否则,在给它赋值之前,您必须重新数组:

Sub my_Procedure() 
ReDim storedata60(I) 
storedata60(I) = ThisWorkbook.Sheets("Sheet6").Range("A1").Value    
'Following code
End Sub