Excel 在哪里声明全局变量

 时间:2026-05-04 12:39:21

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

Excel 在哪里声明全局变量

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

Excel 在哪里声明全局变量

3、我单击执行宏命令,通过按control +Q, 如图,提示框没有给我任何提示

I execute the function by pressing Control+Q, we can see the snapshot that it doesn't show any to me 

Excel 在哪里声明全局变量

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

Excel 在哪里声明全局变量

5、我们再按一下Control+Q会得到提示框,如图所示

we press the control+Q again , we get the expected result as snapshot

Excel 在哪里声明全局变量

  • excel中使用vba新建一个工作表的方法
  • Excel实用技巧-如何批量提取excel工作表名称
  • 如何利用VBA代码自动删除行
  • 在VBA编程中怎样知道打开的工作薄的数量?
  • vba中如何编程打印并保存
  • 热门搜索
    1999年属什么 腰酸痛是什么原因 电器着火用什么灭火器 1893年属什么 雪花像什么 公务员考什么 阿胶是什么做的 称赞的近义词是什么 3月8日是什么星座 风把回忆摇落一地红是什么歌