您好,我正在寻找 VBA 代码,以便从我单击按钮开始每 5 秒使用 VBA 更改一次 Excel 中形状的颜色。我的形状名称是“矩形 1”。
答案1
分配按钮来运行开始计时器:
Public RunWhen As Double
Public Const cRunIntervalSeconds = 5
Public Const cRunWhat = "qwerty"
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
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 qwerty()
Dim s As Shape
Set s = ActiveSheet.Shapes("Rectangle 1")
s.Fill.ForeColor.SchemeColor = Application.WorksheetFunction.RandBetween(1, 20)
Call StartTimer
End Sub