|
- Public currentValue As Integer
- Public direction As Integer
- Public nextTime As Date
- Sub StartLoop()
- currentValue = 1
- direction = 1 ' 1表示递增,-1表示递减
- UpdateValue
- End Sub
- Sub UpdateValue()
- ' 将当前值写入A1单元格
- Range("A1").Value = currentValue
-
- ' 检查是否需要改变方向
- If currentValue = 31 And direction = 1 Then
- direction = -1
- ElseIf currentValue = 1 And direction = -1 Then
- direction = 1
- End If
-
- ' 计算下一个值
- currentValue = currentValue + direction
-
- ' 安排0.5秒后再次执行
- nextTime = Now + TimeSerial(0, 0, 0.5)
- Application.OnTime nextTime, "UpdateValue"
- End Sub
- Sub StopLoop()
- On Error Resume Next ' 避免未计划任务时报错
- Application.OnTime nextTime, "UpdateValue", , False
- End Sub
复制代码
|
|