我正在处理一些VBA代码,并且得到了
参数不是可选的
错误。我浏览了该问题的其他一些答案,但它们似乎并没有回答我的问题。
代码在第一次迭代中卡住了,Sub Finding_number ()
这让我感到困惑。我没有从该Sub传递任何参数,所以为什么会出错?
我的代码:
Sub Pedal_Actuations_per_hour()
Dim counter As Integer
Dim average_actuations As Single
counter = 1
Do While IsEmpty(ActiveCell.Value) = False
Finding_number
average_actuations = (average_actuations + Pedal_actuations()) / counter
counter = counter + 1
Loop
Range("J2").Value = average_actuations
End Sub
Sub Finding_number()
Dim index As Integer
index = 1
Range("E2").Select
Do While index = 1
If ActiveCell.Value = 121 Then
index = 0
End If
Range.Offset (1)
Loop
End Sub
Function Pedal_actuations() As Integer
Dim time_sum As Single
Dim index As Integer
index = 1
time_sum = 0
Do While time_sum < 1
If IsEmpty(ActiveCell.Value) = 0 Then
date_number = Int(ActiveCell(, -2).Value)
ActiveCell(, 6).Value = ActiveCell(, -2).Value - date_number
ActiveCell(, 7).Value = Abs(ActiveCell(, 6).Value - ActiveCell(2, 6)) *24
Else
index = 0
End If
Pedal_actuations = Pedal_actuations + 1
time_sum = time_sum + ActiveCell(, 7).Value
Loop
End Function
最好避免使用Select
,,ActiveCell
等...而是使用引用的对象asSheets
和Range
s。
Sub Finding_number()
Dim index As Integer
index = 1
Dim Rng As Range
Set Rng = Range("E2")
Do While index = 1
If Rng.Value = 121 Then
index = 0
End If
Set Rng = Rng.Offset(1)
Loop
End Sub
下面的较短版本将为您提供相同的结果:
Sub Finding_number()
Dim Rng As Range
Set Rng = Range("E2")
Do While Rng.Value <> 121
Set Rng = Rng.Offset(1)
Loop
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句