2016-09-29 71 views
0

所以我正在編寫一個基本腳本,切換按鈕在選擇宏時一直給我一個運行時錯誤。基本上,我想要做的是切換Manuel的計算,因爲有大量的公式,當輸入新數據時,自動更新將永遠存在,並且它會成爲一個麻煩,這就是爲什麼我想要使用它。它曾經在過去,但現在由於某種原因,它不起作用。我對此很陌生,任何信息都會有所幫助。運行時錯誤 - 切換按鈕

Private Sub Workbook_Open() 

Application.Calculation = xlCalculationAutomatic 
Worksheets("Schedule").CommandButton1.Caption = "Auto Calc: ON" & Chr(10) & "[Click to Toggle]" 
ClacState = False 


End Sub 
+2

什麼是運行時錯誤居然說? –

+2

'ClacState'看起來像拼錯了。如果你使用'Option Explicit',編譯器可以自動捕獲這樣的錯誤。 – xidgel

+0

什麼是'ClacState'?我只知道[Application.CalculationState](https://msdn.microsoft.com/en-us/library/office/ff196047.aspx)。 – Ralph

回答

0

以下的(更詳細)的代碼,你應該能夠找出問題:

Option Explicit 

Private Sub CommandButton1_Click() 

Dim ws As Worksheet 
Dim Obj As OLEObject 
Dim bolFound As Boolean 
Dim ClacState As Boolean 

Application.Calculation = xlCalculationAutomatic 

'Verify that the sheet exists 
bolFound = False 
For Each ws In ThisWorkbook.Worksheets 
    If ws.Name = "Schedule" Then bolFound = True 
Next ws 
If bolFound = False Then 
    MsgBox "The is no such sheet `Schedule`." & Chr(10) & "Aborting..." 
    Exit Sub 
End If 

'Verify that there is a button by that name 
bolFound = False 
For Each Obj In ThisWorkbook.Worksheets("Schedule").OLEObjects 
    If Obj.Name = "CommandButton1" And TypeName(Obj.Object) = "CommandButton" Then bolFound = True 
Next Obj 
If bolFound = False Then 
    MsgBox "The is no such button `CommandButton1` on the sheet `Schedule`." & Chr(10) & "Aborting..." 
    Exit Sub 
End If 

Worksheets("Schedule").CommandButton1.Caption = "Auto Calc: ON" & Chr(10) & "[Click to Toggle]" 

End Sub