简体   繁体   English

公共变量和Workbook_Open函数

[英]Public variables and Workbook_Open function

I want to initialize some variable (like the ranges for graphs) in the Workbook_Open function in ThisWorksheet. 我想在ThisWorksheet的Workbook_Open函数中初始化一些变量(如图形的范围)。 This is done like: 就像这样完成:

Public ChartSizePosition As Range
Sub Workbook_Open()
    Set ChartSizePosition = Range("B8:I25")
    Worksheets("Übersicht").Range("A1").Value = "q3f"
End Sub

Afterwards I want to access the variable from some Sub in a module. 之后,我想从模块中的某些Sub中访问变量。 But there is always given an error "Runtime error 424. Object necessary". 但是总会出现错误“运行时错误424。对象必需”。 The Workbook_Open code is definitely reached. 肯定可以到达Workbook_Open代码。

Anyone can give me a suggestion? 有人可以给我一个建议吗?

Best regards and thanks! 最好的问候和感谢!

If you add a public variable to a class module (which ThisWorkbook is) you have to refer to the variable as a member of that class. 如果将公共变​​量添加到类模块(ThisWorkbook是),则必须将该变量引用为该类的成员。 So you have to refer to ThisWorkbook.ChartSizePosition rather than just ChartSizePosition . 因此,您必须引用ThisWorkbook.ChartSizePosition而不是ChartSizePosition

Alternatively you can move the declaration to a normal module. 或者,您可以将声明移至普通模块。

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

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