所以我正在编写一个基本脚本,当我选择宏时,切换按钮不断给我一个运行时错误。基本上,我想要它做的是关闭和继续计算Manuel,因为有大量的公式,当输入新数据时,自动更新需要永远,它变得麻烦,这就是我想要使用它的原因。它过去曾奏效,但由于某种原因它现在无效。我对此有点新鲜,任何信息都会有所帮助。
Private Sub Workbook_Open()
Application.Calculation = xlCalculationAutomatic
Worksheets("Schedule").CommandButton1.Caption = "Auto Calc: ON" & Chr(10) & "[Click to Toggle]"
ClacState = False
End Sub
答案 0 :(得分: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