工作表和模块之间的变量不合作 [英] Variable between worksheet and module not cooperating

查看:82
本文介绍了工作表和模块之间的变量不合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Module1宏没有看到Worksheet代码中声明的变量的值。任何想法为什么呢?一切都是公开的



在我的工作表中我有:

 公共oldVal作为变式

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
oldVal = Target.Value
End Sub

在Module1中,我有一个宏连接到一个按钮,应该(为了测试目的)打印oldVal,但它不工作。消息框为空。

  Public Sub button_Click1()
MsgBox oldVal
End Sub

任何想法为什么它不从工作表中获取变量OldVal的值?



谢谢。

解决方案

您应该在常规模块中声明您的公共变量以避免这个。



因为Sheet的模块,ThisSession,ThisWorkbook,...是类的模块,因此您声明的变量被认为是该类/对象的属性。 / p>




您的常规模块代码

  Public oldVal As Variant 

Public Sub button_Click1()
MsgBox oldVal
End Sub

您的工作表模块代码

  Public Sub Worksheet_SelectionChange(ByVal Target As Range)
oldVal = Target.Value
End Sub


My Module1 macro doesn't see the value of variable stated in Worksheet code. Any idea why that is? Everything is public.

In my Worksheet I have this:

Public oldVal As Variant

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
oldVal = Target.Value
End Sub

And in Module1 I have a macro connected to a button, that should (for testing purposes) just print the oldVal, but it isn't working. The message box is empty.

Public Sub button_Click1()
MsgBox oldVal
End Sub

Any idea why it isn't taking the value of the variable "OldVal" from the worksheet?

Thanks.

解决方案

You should declare your Public variables in a regular module to avoid this.

Because Sheet's modules, ThisSession, ThisWorkbook, ... are class's modules and so the variable that you declare is considered as a property of that class/object.


Your regular module code

Public oldVal As Variant

Public Sub button_Click1()
    MsgBox oldVal
End Sub

Your Sheet module code

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
     oldVal = Target.Value
End Sub

这篇关于工作表和模块之间的变量不合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆