Excel 在哪里声明全局变量
1、声明一个变量并且给它赋值。
Declare a various and assign value by selectionchange event.
Option Explicit
Public myName As String
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
myName = Target.Address
End Sub
2、定义一个宏函数,用于显示全局变量是否有值
Define a function to display the value of global various such that we can see if work or not
Sub Macro1()
'
' Macro1 Macro
' 宏由 微软中国 录制,时间: 2015-11-14
'
' 快捷键: Ctrl+q
'
MsgBox myName
End Sub
3、我单击执行宏命令,通过按control +Q, 如图,提示框没有给我任何提示
I execute the function by pressing Control+Q, we can see the snapshot that it doesn't show any to me
4、那么我们改一下全局变量声明的位置
so we change the position of global various
Option Explicit
Public myName As String
Sub Macro1()
'
' Macro1 Macro
' 宏由 微软中国 录制,时间: 2015-11-14
'
' 快捷键: Ctrl+q
'
MsgBox myName
End Sub
5、我们再按一下Control+Q会得到提示框,如图所示
we press the control+Q again , we get the expected result as snapshot