Excel手动计算工作表以外的其他活动

时间:2015-07-18 10:52:45

标签: excel vba excel-vba

我正在尝试设置工作簿以进行速度优化的手动计算。主要是我想在更改器件应用之后只计算当前工作表,所以我正在使用:

public class Category
{
    public String Name { get; set; }
    public String Description { get; set; }
}

但是我有一张表需要手动计算两张纸 - 当前和第二张。我尝试了Private Sub Worksheet_Activate() Application.Calculate Application.Calculation = xlCalculationManual End Sub Private Sub Worksheet_Change(ByVal Target As Range) ActiveSheet.Calculate End Sub Sheet(4).Calculate的不同方式。似乎只有ActiveSheet.Calculate正在运行。我该怎么办?

1 个答案:

答案 0 :(得分:2)

如果您不想要计算特定工作表,则需要删除Application.Calculate

Private Sub Worksheet_Activate()
    Application.Calculation = xlCalculationManual
End Sub

并指定要计算的两张纸。包含3张不计算Sheet1的文件:

Sheet1模块(当Sheet1更改时):

Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheets(2).Calculate
    Worksheets(3).Calculate
End Sub

Sheet2模块(当Sheet2更改时):

Private Sub Worksheet_Change(ByVal Target As Range)
    Me.Calculate
    Worksheets(3).Calculate
End Sub

Sheet3模块(当Sheet3更改时):

Private Sub Worksheet_Change(ByVal Target As Range)
    Worksheets(2).Calculate
    Me.Calculate
End Sub

以下是我用来优化Excel速度设置的实用程序

应用程序级别设置:

Option Explicit

Public Sub fastWB(Optional ByVal opt As Boolean = True)

    With Application
        .Calculation = IIf(opt, xlCalculationManual, xlCalculationAutomatic)
        If .DisplayAlerts <> Not opt Then .DisplayAlerts = Not opt
        If .DisplayStatusBar <> Not opt Then .DisplayStatusBar = Not opt
        If .EnableAnimations <> Not opt Then .EnableAnimations = Not opt
        If .EnableEvents <> Not opt Then .EnableEvents = Not opt
        If .ScreenUpdating <> Not opt Then .ScreenUpdating = Not opt
    End With

    fastWS , opt

End Sub

工作表级别设置:

Public Sub fastWS(Optional ByVal ws As Worksheet, Optional ByVal opt As Boolean = True)
    If ws Is Nothing Then
        For Each ws In ThisWorkbook.Worksheets
            setWS ws, opt
        Next
    Else
        setWS ws, opt
    End If
End Sub

Private Sub setWS(ByVal ws As Worksheet, ByVal opt As Boolean)
    With ws
        .DisplayPageBreaks = False
        .EnableCalculation = Not opt
        .EnableFormatConditionsCalculation = Not opt
        .EnablePivotTable = Not opt
    End With
End Sub

Excel默认设置:

Public Sub xlResetSettings()    'default Excel settings
    With Application
        .Calculation = xlCalculationAutomatic
        .DisplayAlerts = True
        .DisplayStatusBar = True
        .EnableAnimations = False
        .EnableEvents = True
        .ScreenUpdating = True
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            With ws
                .DisplayPageBreaks = False
                .EnableCalculation = True
                .EnableFormatConditionsCalculation = True
                .EnablePivotTable = True
            End With
        Next
    End With
End Sub

注意:您的代码应在处理完成后将设置恢复为初始值