EXCEL VBA 定义变量全局

2024-11-22 23:20:01
推荐回答(4个)
回答1:

声明全局变量可以在文档任何位置(For循环、If判断内除外)。比如:
Option Explicit
Public a As String
Sub aa()
a = "public"
MsgBox a
End Sub
以上代码可执行。

Option Explicit
Public a As String
Sub aa()
a = "public"
MsgBox a
Dim i As Single
For i = 1 To 6
Public b As Single
b = i
MsgBox b
Next
End Sub
以上代码提示:
编译错误:
Sub 或 Function 中的属性无效
以上。

回答2:

两个方法,第一个方法,定义全局变量,就是在程序最开头的地方写
public asc as long
还有一个方法就是进行参数传递。
SUB AA(ACS as long)
ACS=9
END SUB

SUB AB(ACS as long)
ACS=9+1
END SUB

SUB AC(ACS as long)
MSGBOX ASC 'ASC的值为10
END SUB

调用的时候都要将acs的地址传递给过程。

回答3:

Public ASC As Integer
该语句定义变量
还需要给变量赋值啊
如:ASC=10

回答4:

Public acs As Long
Sub AA()
acs = 9
End Sub

Sub AB()
acs = acs + 1
End Sub

Sub AC()
Call AA
Call AB
MsgBox acs
End Sub
运行AC即可
------
Public acs As Long
Sub AA()
acs = 9
AB acs
End Sub

Sub AB(acs)
acs = acs + 1
AC acs
End Sub

Sub AC(acs)
MsgBox acs
End Sub
运行AA即可