
こんにちは。ボタンがクリックされてから 5 秒ごとに VBA を使用して Excel の図形の色を変更する VBA コードを探しています。図形の名前は「Rectangle 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