師も走りまわる12月。。皆様(師)も走りまわっていることでしょう。 私は。。。走りません、はい。 本題です。 ExcelのVBAでToggleButtonを排他的に使いたいのです。 調べるとオプションボタンになさいと書かれてはいるのですが・・・。 ワークシート上にToggleButton1~3があります。 1を押す(凹)と“処理A”,もう一度押す(凸)と“処理Z” 2 同上 “処理B”, 同上 3 同上 “処理C”, 同上 以下のコードでなんとなく動くのですが,排他的動作の際には,TrueなToggleButton#の動作をおこなってしまいます。 Excel2007,Windows7です。 師の皆様の良い御提案が御座いましたら御教示下さい。 ----ボタンがあるシートモジュールに記載---------------------------------------- Option Explicit Dim strCode As Variant Private Sub ToggleButton1_Click() If ToggleButton1.Value = False Then strCode = "処理中Z" ' Debug.Print "AからZに戻します。" Else strCode = "処理中A" ' Debug.Print "Aを押しました。" ToggleButton2.Value = False ToggleButton3.Value = False End If 'Debug.Print "Aでの処理をします。" Call 処理(strCode) 'Debug.Print "Aから処理をしました。" End Sub Private Sub ToggleButton2_Click() If ToggleButton2.Value = False Then strCode = "処理中Z" ' Debug.Print "BからZに戻します。" Else strCode = "処理中B" ' Debug.Print "Bを押しました。" ToggleButton1.Value = False ToggleButton3.Value = False End If 'Debug.Print "Bでの処理をします。" Call 処理(strCode) 'Debug.Print "Bから処理をしました。" End Sub Private Sub ToggleButton3_Click() If ToggleButton3.Value = False Then strCode = "処理中Z" Else strCode = "処理中C" ToggleButton1.Value = False ToggleButton2.Value = False End If Call 処理(strCode) End Sub Sub 処理(strCode) Dim i As Long Dim Start As Single Dim Finish As Single Start = timer i = 0 'Application.ScreenUpdating = False Application.Calculation = xlCalculationManual For i = 1 To 1000 i = i + 1 Cells(2, 1).Value = strCode & i Next i Application.Calculation = xlCalculationAutomatic 'Application.ScreenUpdating = True Cells(1, 1).Value = strCode Finish = timer Cells(3, 1).Value = Format$(Int((Finish - Start) * 10 ^ 4 + 0.5) / 10 ^ 4) End Sub --------------------------------------------------------------------------- おまけ: 上記サンプルを作ってみましたが,ToggleButton1だけ相対的に処理速度が速いです・・・。 なぜなんでしょう??
↧