简体   繁体   English

Excel VBA中的公共变量

[英]Public Variables in excel vba

How do I declare 2 "Public" variables in the declaration section of "ThisWorkbook" so they are available across all modules after the worksheet is opened and until it is closed? 如何在“ ThisWorkbook”的声明部分中声明2个“公共”变量,以便在工作表打开后直至关闭之前,所有模块都可以使用它们? Tried "Public WarningDate As Date, ExpirationDate As Date", but only the first, WarningDate, is available. 尝试使用“公共WarningDate作为日期,ExpirationDate作为日期”,但是只有第一个WarningDate可用。 Is there only 1 declaration line available? 只有1条声明行可用吗? Can I list more than 1? 我可以列出多个吗? If so, how? 如果是这样,怎么办?

Thank you. 谢谢。

You can use variables in object modules this way: 您可以通过以下方式在对象模块中使用变量:

Worksheet module (say, Sheet1): 工作表模块(例如Sheet1):

Public BigTime As Date, LittleTime As Date

Any module: 任何模块:

MsgBox Sheet1.BigTime
MsgBox Sheet1.LittleTime

Object Modules 对象模块

It is not possible the way you're trying to do it. 您尝试执行此操作的方式是不可能的。

You have to differentiate 'normal' modules (via add module) and object modules (ThisWorkbook, Sheet1, etc.). 您必须区分“普通” 模块 (通过添加模块)和对象模块(ThisWorkbook,Sheet1等)。

If you declare a public variable in a 'normal' module, it will be 'visible' to all procedures of all modules including object modules as it is eg: 如果在“普通”模块中声明了公共变量,则它对于包括对象模块在内的所有模块的所有过程都是“可见的”,例如:

WarningDate
ExpirationDate

If you declare a public variable in an object module, it will be 'visible' to all procedures of the object module as it is eg: 如果一个对象模块中声明公用变量,这将是“看得见”的对象模块的所有过程,因为它是例如:

WarningDate
ExpirationDate

and it will be 'visible' to all other modules including object modules, but you have to use its object reference to refer to it, eg: 并且它将对所有其他模块(包括对象模块)“可见”,但是您必须使用其对象引用来引用它,例如:

ThisWorkbook.WarningDate
ThisWorkbook.ExpirationDate

Conclusion 结论

You correctly declared the variables, but you didn't know how to refer to them. 您正确声明了变量,但是您不知道如何引用它们。 If you want public variables accessed without object references add (or pick) a module ( not object module ) and declare them there. 如果要在没有对象引用的情况下访问公共变量,请添加(或选择)一个模块而不是对象模块 )并在其中声明它们。

Visualize 可视化

' Code in ThisWorkbook
Option Explicit

Public ThisWorkbookDate As Date

Sub MainThisWorkbook()
  MsgBox ThisWorkbookDate
  MsgBox Module1Date
  MsgBox Sheet1.Sheet1Date
End Sub

' Code in Module1
Option Explicit

Public Module1Date As Date

Sub MainModule1()
  MsgBox ThisWorkbook.ThisWorkbookDate
  MsgBox Module1Date
  MsgBox Sheet1.Sheet1Date
End Sub

' Code in Sheet1
Option Explicit

Public Sheet1Date As Date

Sub MainThis() 'OK
  MsgBox ThisWorkbook.ThisWorkbookDate
  MsgBox Module1Date
  MsgBox Sheet1Date
End Sub

Thanks to everyone for the help. 感谢大家的帮助。 I ended up just placing the dates in a locked, hidden cell in the locked first worksheet, then referring to it in various macros and in an open workbook command. 我最终只是将日期放置在锁定的第一个工作表的锁定的隐藏单元格中,然后在各种宏和打开的工作簿命令中对其进行引用。 Each of these macros re-declares the variable with these cells values. 这些宏中的每一个都使用这些单元格值重新声明该变量。 Not the most efficient, but it works and I only need to update 3 cell dates to change the triggers. 并非最有效,但它可以工作,我只需要更新3个单元格日期即可更改触发器。 The code I used is ... 我使用的代码是...

Private Sub Workbook_Open()

'Usage Rights Code
        Static WarningDate As Date
        WarningDate = Worksheets("XXX").Cells(1, 1).Value
        Static ExpirationDate As Date
        ExpirationDate = Worksheets("XXX").Cells(2, 1).Value
        Static LockDate As Date
        LockDate = Worksheets("XXX").Cells(3, 1).Value
        '
        If Date >= WarningDate And Date < ExpirationDate Then
            MsgBox "Usage rights for this workbook will expire on " _
                & ExpirationDate & ". After the expiration date, critical " _
                & "calculations will be disabled. Please contact ABC Co." _
                & "to avoid de-activation."
        ElseIf Date >= ExpirationDate And Date < LockDate Then
            MsgBox "Usage rights for this workbook expired on " _
                & ExpirationDate & ". Critical calculations have been " _
                & "disabled. The workbook will be locked on " & LockDate _
                & ". Please contact ABC Co. for re-activation."
        ElseIf Date >= LockDate Then
            MsgBox "Usage rights for this workbook expired on " _
                & ExpirationDate & ". The workbook has been disabled and " _
                & "locked. Please contact ABC Co. for re-activation."
            ThisWorkbook.Close savechanges:=False
        End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM