DLL“' - t”给出运行时错误424:对象必需

时间:2015-07-14 15:08:39

标签: vba excel-vba excel-2010 excel

我正在使用一个简单的计时器,告诉我在使用不同数据类型执行相同计算之间经过的时间。当我运行这个时,我收到错误:

  

运行时错误'424':

     

需要对象

麻烦的路线:

target_sheet.Range("A2").Value = -t

以下是我的所有代码:

Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
Sub Function1_Var_RandNumCounter()

Dim Var_RandNum_X, Var_RandNum_Y, Count As Variant

For Count = 1 To Count = 1000000
    Var_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
    Var_RandNum_Y = Rnd(Now)
Next Count

target_sheet.Range("A2").Value = -t

'   MsgBox GetTickCount - t, , "Milliseconds"
Call Function1_Dec_RandNumCounter

End Sub
Sub Function1_Dec_RandNumCounter()

Dim Count, Var_RandNum_X, dec_RandNum_X, Var_RandNum_Y, dec_RandNum_Y

dec_RandNum_X = CDec(Var_RandNum_X)
dec_RandNum_Y = CDec(Var_RandNum_Y) ' convert these vals to decimals

For Count = 1 To Count = 1000000
    dec_RandNum_X = Rnd(Now) ' Get rnd vals based on Now, built-in VBA property
    dec_RandNum_Y = Rnd(Now)
Next Count

target_sheet.Range("B2").Value = -t

'   MsgBox GetTickCount - t, , "Milliseconds"
Call Function1_Int_RandNumCounter

End Sub
Sub Function1_Int_RandNumCounter()

Dim Count, Int_RandNum_X, Int_RandNum_Y

For Count = 1 To Count = 1000000
    Int_RandNum_X = Rnd(Now)
    Int_RandNum_Y = Rnd(Now)
Next Count

target_sheet.Range("C2").Value = -t

'   MsgBox GetTickCount - t, , "Milliseconds"

Call Function1_Double_RandNumCounter

End Sub
Sub Function1_Double_RandNumCounter()

Dim Count, Dbl_RandNum_X, Dbl_RandNum_Y

For Count = 1 To Count = 1000000
    Dbl_RandNum_X = Rnd(Now)
    Int_RandNum_Y = Rnd(Now)
Next Count

target_sheet.Range("D2").Value = -t

'   MsgBox GetTickCount - t, , "Milliseconds"

End Sub
Sub Function2_BarGraph()
'   Put all of these vals in a 2D bar graph
End Sub

1 个答案:

答案 0 :(得分:1)

可能的原因是你还没有为变量target_sheet分配任何东西,所以它是空的,当它为空时你不能调用它的方法(即Range),因为它会导致'运行时错误424 - 需要对象'。

在第一次使用它之前,您需要将一些工作表分配给变量target_sheet,即:

Set target_sheet = ActiveSheet
target_sheet.Range("A2").Value = -t