Run this Worksheet_Calculate when opening workbook without running my Macro

时间:2019-01-18 18:44:54

标签: excel vba

Right now my process is working correctly, with the help of this community, however, I need this Worksheet_Calculate to NOT execute the Macro (MacroRuns for example) when the workbook opens, but I still need it to function the same way it is currently, after the workbook is opened.

Thank you so much for your help in advance!

The Code I Am Using:

in ThisWorkbook

Option Explicit

Private Sub Workbook_Open()
    TargetStart
End Sub

in the target sheet's code window

Option Explicit

Private Sub Worksheet_Calculate()
Application.EnableEvents = False
    TargetCalc Me
Application.EnableEvents = True
End Sub

in Module 1

Option Explicit

Public TargetValue As Variant
Private Const cTarget As String = "C3"

Sub TargetCalc(ws as Worksheet)
    If ws.Range(cTarget) <> TargetValue Then
'this is where I would like the code to say something like, "if workbook just opened, exit -- otherwise continue. If this is even possible.
       Call MacroRuns
        TargetValue = ws.Range(cTarget).Value
    End If
End Sub

Sub TargetStart()
    TargetValue = Sheet1.Range(cTarget).Value
End Sub


Sub MacroRuns()

    Call UpdateMsgBox

End Sub

2 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题:

在此工作簿中:

select sum(case when level = 'Classic' then OneMeansActive else 0 end) as numClassicActives,
       sum(case when level = 'Basic' then OneMeansActive else 0 end) as numBasicActives,
       sum(case when level = 'Classic' then subInactive else 0 end) as numClassicSubinactives,
       sum(case when level = 'Classic' then subInactive else 0 end) as numBasicSubinactives  
from company_members m

在目标工作表的代码窗口中:

Private Sub Workbook_Open()
    Worksheets("NameHere").Range("A1") = True
End Sub

答案 1 :(得分:0)

请尝试这种安排。

Public StartUp As Boolean

Private Sub Workbook_Open()
    StartUp = True
    TargetStart
End Sub

Sub TargetCalc(ws As Worksheet)
    If ws.Range(cTarget) <> TargetValue Then
        If Not StartUp Then MacroRuns
        StartUp = False
        TargetValue = ws.Range(cTarget).Value
    End If
End Sub

或者,也许您更喜欢这样。

Sub TargetCalc(ws As Worksheet)
    If ws.Range(cTarget) <> TargetValue Then
        If Not StartUp Then
            MacroRuns
            TargetValue = ws.Range(cTarget).Value
        End If
        StartUp = False
    End If
End Sub