2016-09-29 73 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