我有下面的代码,关闭自动计算,我找不到为什么这样做的原因。我使用的是Office 2016,之前使用的是2007年发布的那个版本,但是它并不知道从哪个宏里找到触发更改但仍然不知道为什么的宏。任何帮助澄清这一点将不胜感激。为什么自动计算由VBA宏关闭?
Public Sub editAllSheets()
'
'
'
Dim myResult As VbMsgBoxResult
Dim WS As Worksheet
myResult = MsgBox("Are you sure you want to edit all sheets in this workbook?", vbQuestion + vbYesNo + vbDefaultButton1, "Edit Workbook?")
If myResult = vbNo Then Exit Sub
On Error GoTo ErrorHandler
For Each WS In ActiveWorkbook.Worksheets
WS.Activate
Application.ScreenUpdating = False
editingProperties WS
Application.ScreenUpdating = True
Next WS
Sheets.Select
MsgBox "Please note:" & vbNewLine & vbNewLine & "1. All the sheets are selected." & vbNewLine & "2. Proceed with print preview to view and print all reports." & vbNewLine & "3. To print preview or print only 1 report of this workbook you need to click on a different sheet to deselect all.", vbInformation, "Process Completed!"
Exit Sub '<--- exit here if no error occured
ErrorHandler:
Debug.Print Err.Description
Application.ScreenUpdating = True
MsgBox "Sorry, an error occured." & vbCrLf & Err.Description, vbCritical, "Error!"
End Sub
Private Sub editingProperties(WS As Worksheet)
Dim columnsToDelete As Range
With WS
.Columns("A:F").UnMerge
Set columnsToDelete = Application.Union(.Columns("B:C"), _
.Columns("F:K"), _
.Columns("P:R"), _
.Columns("V:W"))
columnsToDelete.Delete
.Cells.EntireColumn.AutoFit
.Range("A1:B2").Merge
End With
With WS.PageSetup
.PrintArea = ""
.PrintTitleRows = ""
.PrintTitleColumns = ""
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.25)
.RightMargin = Application.InchesToPoints(0.25)
.TopMargin = Application.InchesToPoints(0.75)
.BottomMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.3)
.FooterMargin = Application.InchesToPoints(0.3)
.Orientation = xlLandscape
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
End Sub
在过去,我用下面的代码,使宏快,但我注意到,从自动计算到手动的改变,但不运行任何东西,所以我删除,不知道它是否仍然是相关的。任何帮助解决这个问题将非常感激。
Option Explicit
Sub OptimizeVBA(isOn As Boolean)
Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
Application.EnableEvents = Not(isOn)
Application.ScreenUpdating = Not(isOn)
ActiveSheet.DisplayPageBreaks = Not(isOn)
End Sub
'Some macro
Sub ExampleMacro()
OptimizeVBA True
'Your code here
OptimizeVBA False
End Sub
你可能想看看这个thr3ead:https://answers.microsoft.com/en-us/office/forum/office_2007-excel/the-calculation-option-keeps-switching-from -auto/2ed29346-b946-4aaf-9c19-83911eea812e有一个说法,即如果在会话期间打开的第一个工作簿将计算设置为手动,则所有其他工作簿都将继承此设置。因此,您应该查看其他关闭自动计算的工作簿,并确保它们在关闭之前再次打开。 – Variatus
@Variatus我曾经有一个宏,关闭自动计算,但我删除它,现在我有一台新电脑,并将个人宏工作簿移动到新的电脑,但我已经检查,并用宏来关闭自动计算不存在,因为我已经删除之前,移动到新的电脑。所以我的问题是为什么还在发生? – QuickSilver
@ Variatus \t 为什么'Sheets.Selec'行会触发自动计算到手动是否有原因?我有重新编译和没有错误,并调试它,当代码击中'表。选择它现在自动变为手动为什么是这样?我已经注释掉了这条线,计算模式仍然是自动的,但我不知道它为什么这样做。 – QuickSilver