简体   繁体   English

在Workbook_open上创建并分配变量,然后将其传递给Worksheet_change

[英]Create and assign variables on Workbook_open, pass it to Worksheet_change

So I have a Workbook_open sub that creates Long variables when workbook is opened: 因此,我有一个Workbook_open子项,该子项在打开工作簿时创建Long变量:

Private Sub Workbook_open()
  MsgBox ("Workbook opened")

  Dim i As Long
  i = 68 
End Sub

How would I pass the i value to a Worksheet_change sub for a particular worksheet? 如何将i值传递给特定工作表的Worksheet_change子项?

Dim i inside a procedure scope makes i a local variable; 过程范围内的Dim i使i成为局部变量; it's only accessible from within the procedure it's declared in. 它只能在声明它的过程中访问。

The idea of passing i to another procedure is very sound: it means you intend to keep variable scopes as tight as possible, and that's a very very good thing. i传递给另一个过程的想法非常合理:这意味着您打算尽可能使可变范围保持紧密,这是非常非常好的事情。

But in this case it's too tight, because the parameters of an event handler are provided by the event source: you need to "promote" that local variable up one scope level. 但在这种情况下,它是紧张,因为事件处理程序的参数由事件源提供:您需要在“促进”是局部变量了一个范围级别。

And the next tightest scope level is module scope . 第二个最严格的作用域级别是模块作用域

You can use the Dim keyword at module level, but for consistency's sake I'd recommend using the keyword Private instead. 可以在模块级别使用Dim关键字,但是为了保持一致性,我建议改用关键字Private So in the same module , declare your module-level variable: 因此,在同一模块中 ,声明您的模块级变量:

Option Explicit
Private i As Long

Private Sub Workbook_open()
  MsgBox "Workbook opened"
  i = 68 
End Sub

If you want to expose that variable's value beyond this module, you can expose an accessor for it: 如果要在此模块之外公开该变量的值,则可以公开其访问器:

Option Explicit
Private i As Long

Private Sub Workbook_open()
  MsgBox "Workbook opened"
  i = 68 
End Sub

Public Property Get MyValue() As Long
'invoked when MyValue is on the right-hand side expression of an assignment,
'e.g. foo = ThisWorkbook.MyValue
    MyValue = i
End Property

Now the Sheet1 module's Worksheet_Change handler can read it: 现在, Sheet1模块的Worksheet_Change处理程序可以读取它:

Private Sub Worksheet_Change(ByVal Target As Range)
    MsgBox ThisWorkbook.MyValue
End sub

But it can't write to it, because the property is "get-only". 但是它无法写入 ,因为该属性是“仅获取”的。 If everyone everywhere needs to be able to read/write to it, then you might as well make it a global variable , or expose a mutator for it: 如果每个人都到处需要能够读/写,那么你可能也使它成为一个全局变量 ,或为它赋值函数

Option Explicit
Private i As Long

Private Sub Workbook_open()
  MsgBox "Workbook opened"
  i = 68 
End Sub

Public Property Get MyValue() As Long
'invoked when MyValue is on the right-hand side expression of an assignment,
'e.g. foo = ThisWorkbook.MyValue
    MyValue = i
End Property

Public Property Let MyValue(ByVal value As Long)
'invoked when MyValue is on the left-hand side of an assignment,
'e.g. ThisWorkbook.MyValue = 42; the 'value' parameter is the result of the RHS expression
    i = value
End Property

For that, declare i as a Public Variable on a Standard Module like Module1 and then you can access the value of i in Sheet Change Event if it is initialized during Workbook Open Event . 为此,将i声明为诸如Module1类的Standard Module上的Public Variable ,然后如果在Workbook Open Event对其进行了initialized ,则可以在Workbook Open Event Sheet Change Event访问i的值。

On Standard Module: 在标准模块上:

Public i As Long

On ThisWorkbook Module: 在此工作簿模块上:

Private Sub Workbook_open()
  MsgBox ("Workbook opened")
  i = 68
End Sub

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

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