プロが教える店舗&オフィスのセキュリティ対策術

エクセルVBAで、
スタートボタンを押すと値の入ってるセルを自動的に移動していって、ストップボタンを押すと止まる。
そんなルーレットみたいなものって作れますか???

A 回答 (7件)

ストップボタンはないけれど、こんなの作ってみました。


まっさらなワークシートで試してみてください。

Sub ルーレット()
Range("B3:K9").Select
With Selection
.Font.Name = "Arial Black"
.Font.Size = 20
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
ActiveWorkbook.Names.Add Name:="table", RefersTo:=Selection
n = 0
For Each c In Range("table")
n = n + 1
c.Value = n
Next
For i = 1 To 2
For Each c In Range("table")
c.Select
For m = 20 To 3 Step -1
Selection.Interior.ColorIndex = m
Next m
Range("table").Interior.ColorIndex = 0
Next c
Next i
Randomize
x = Int(Rnd * 70) + 1
For Each c In Range("table")
c.Select
For m = 20 To 3 Step -1
Selection.Interior.ColorIndex = m
Next m
Range("table").Interior.ColorIndex = 0
If c.Value = x Then
Selection.Interior.ColorIndex = 3
Exit Sub
End If
Next c
End Sub
    • good
    • 0

ワークシートSheet1にオートシェイプの中から太い矢印を貼り付けました。


「オートシェイプ 2」という名前でした。
もうひとつ、コマンドボタンを貼り付けました。
Sheet1のコマンドボタンのクリックイベントに
Private Sub CommandButton1_Click()
Worksheets("sheet1").Range("a1").Activate
For i = 2 To 10
t0 = Timer
Do While Timer < t0 + 1
DoEvents
'ActiveSheet.Cells(i - 1, "A") = ""
'Cells(i, "A") = "A"
Worksheets("sheet1").Shapes("オートシェイプ 2").Left = i * 50
Loop
Next i
End Sub
を貼り付けます。
ボタンをクリックするとかくかくと10秒間、矢印が動いていきます。
スムーズにするには1秒をもっと短くする。
太い矢印を、イメージのピクチャなどに変える。
とめるのはとめるフラグを聞いていて、ONならシェイプをDeleteする。コンなのはどうでしょう。やはり道具立ての力不足ですね。
オフィス系などでなく・エクセルVBA以外のプログラム言語に目を向けられることを。
    • good
    • 1

No5です。

こっちのほうがいいかも

Sub ルーレット開始()
Cells(1, 1) = ""
Range("B3:K9").Select
With Selection
.Font.Name = "Arial Black"
.Font.Size = 20
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
ActiveWorkbook.Names.Add Name:="table", RefersTo:=Selection
Range("table").Interior.ColorIndex = 0
n = 0
For Each c In Range("table")
n = n + 1
c.Value = n
Next
Do
For Each c In Range("table")
c.Select
If Cells(1, 1) = "stop" Then
Selection.Interior.ColorIndex = 3
Exit Sub
End If
DoEvents
Next c
Loop
End Sub

Sub ルーレットSTOP()
Cells(1, 1) = "stop"
End Sub
    • good
    • 0

No4さんのをそのまま拝借してストップボタンを付けてみました。



Sub ルーレットSTART()
Cells(1, 1) = ""
Range("B3:K9").Select
With Selection
.Font.Name = "Arial Black"
.Font.Size = 20
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
ActiveWorkbook.Names.Add Name:="table", RefersTo:=Selection
n = 0
For Each c In Range("table")
n = n + 1
c.Value = n
Next
Do While Cells(1, 1) = ""
For Each c In Range("table")
c.Select
If Cells(1, 1) = "stop" Then
Selection.Interior.ColorIndex = 3
Exit Sub
End If
DoEvents
Range("table").Interior.ColorIndex = 0
Next c
Loop
End Sub

Sub ルーレットSTOP()
Cells(1, 1) = "stop"
End Sub
    • good
    • 1

再びこんにちは。


VBAからワークシートをいじる場合、VBで言うところのタイマーコントロールが使えません。
従って、スタートボタンを押したらループでグルグル回し、その間にストップボタンも受け付け、ストップされたらループから抜けるという処理になるはず。
答えを言えば、ループの中で DoEvents を発行してやれば済むと思います。
しかしスピードの調整等、結構シビアかもしれませんね。
    • good
    • 0

もちろん作れます。


ただちょっとコツがいるかもしれません。
    • good
    • 0
この回答へのお礼

回答有難うございます。
コツとは?

お礼日時:2005/07/22 14:22

可能です。


しかもそう難しくもないでしょう。
    • good
    • 0
この回答へのお礼

回答有難うございます。
実際どうやればいいですか?

お礼日時:2005/07/22 14:21

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!