设置公共变量无法在之前关闭子例程中识别

时间:2019-04-01 00:26:28

标签: variables global-variables public

根据工作簿的名称,我希望能够使用Private Sub Workbook_BeforeClose(取消为布尔值)关闭工作簿时跳过一些步骤。

当我在某些测试子例程中测试变量代码时,变量值正确更新,但是当我在关闭前请求变量值时,它无法识别在主工作簿子例程开头设置的变量值。

'here I set the variable
Public intA As Integer
Private Sub WorkBook_Open()
Dim WbookCheck As Workbook

    On Error Resume Next
    Set WbookCheck = Workbooks("book1.xlsm")
    On Error GoTo 0

    If WbookCheck Is Nothing Then 'not open....
    MsgBox "Variable has been set"
intA = 2    
End If


'Now here is where i want the variable intA to keep its value of 2

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If intA = 2 Then GoTo File_Wrong
Application.Run "Module1.Full_Access"
Application.Run "Module1.No_Access"
Sheets("Welcome").Visible = True
ActiveWorkbook.Close SaveChanges:=True
MsgBox "Sheet will close. It has been reset & saved"
End
File_Wrong:
MsgBox "file wrong - sheet has not been saved!"
With ThisWorkbook
      .Save
      .Close SaveChanges:=False
    End With
    Application.Quit
End Sub

0 个答案:

没有答案