Worksheet_Activate未运行

时间:2014-07-11 16:18:57

标签: excel vba

我在访问数据库中有大表,其中包含一些函数来处理某些数字(工作正常)。每次在此补偿电子表格中更改单元格时,打开和关闭数据库的速度太慢。我想在打开工作表时打开数据库,并在工作表关闭时关闭数据库。我得到的错误是"运行时错误' 91':对象变量或没有设置块变量。"调试将我带到我的appsAccess.Run行,当我在下面的代码中包含以打开和关闭调用db来执行计算的实际子数据库中的数据库时,该行正常工作(每次在其中一个数字中输入数字时打开/关闭太慢91个细胞)。我的代码是:

Option Explicit
Public appAccess As Access.Application

Private Sub Worksheet_Activate()
    Call OpenCompensationDB
End Sub

Private Sub Worksheet_Deactivate()
    Call CloseCompensationDB
End Sub

Private Sub CloseCompensationDB()
    With appAccess
        .CloseCurrentDatabase
        .Quit
    End With
End Sub

Private Sub OpenCompensationDB()
    With appAccess
        .OpenCurrentDatabase "C:\MyPath\Compensation.ACCDB"
        .Visible = False 'Useful for debugging
    End With
End Sub

似乎永远不会运行激活或取消激活代码。我尝试过使用Public,Private,Stop和Breakpoints但没有运气。 2013信托中心没有任何问题。我处于停滞状态。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Option Explicit

Public appAccess As Access.Application

Private Sub OpenCompensationDB()
    Set appAccess = New Access.Application
    With appAccess
        .OpenCurrentDatabase "C:\MyPath\Compensation.ACCDB"
        .Visible = False 'Useful for debugging
    End With
End Sub

Private Sub AutoComp(WritingLevel)
    Select Case WritingLevel
        Case "REP"
            WritingLevelColumn = 10
        Case "SRP"
            WritingLevelColumn = 11
    End Select
    'Debug.Print appAccess.Run("AutoComp", WritingLevel)
    Cells(8, WritingLevelColumn).Value = appAccess.Run("AutoComp", WritingLevel) * Cells(8, _ (WritingLevelColumn - 8)).Value
End Sub

Private Sub Worksheet_Activate()
    Call OpenCompensationDB
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B4:H16")) Is Nothing Then
        Select Case Target.Column
            Case "2"
                WritingLevel = Cells(2, "B").Value
            Case "3"
                WritingLevel = Cells(2, "C").Value
        End Select
        'Debug.Print WritingLevel
        Select Case Target.Row
            Case "8"
                Product = "Auto"
                AutoComp (WritingLevel)
            Case "9"
                Product = "Home"
                HomeComp (WritingLevel)
        End Select
    End If
End Sub