在Excel的VBA里如何定义公共变量?

2024-12-21 16:16:13
推荐回答(1个)
回答1:

你把声明语句以外的代码放在sub ...end sub外面当然会"编译错误: 不正确的外部程序"了

可以先写一个公共过程,public sub 过程名...end sub

例:
public sub abc
Dim Fer As String
Dim Axal, Axaw, Axsl, Axsw, Aoal, Aoaw, Aoql, Aoqw, r701, r700, Yer, Zer As Double
If Zer > r700 And (Fer = "L" Or Fer = "l") Then
r701 = Zer
ElseIf Yer > r700 And (Fer = "W" Or Fer = "w") Then
r701 = Yer
Else
r701 = r700
End If
Axql = Int((Axsl + Axsw + Aoal) / Aoaw * Aoql - Aoqw)
Axqw = Int((Axsl + Axsw) * (Aoal - Aoaw) / Aoql)
end sub

然后在工程内的其它过程或函数中通过过程名调用以上过程
例:
sub aaa
代码段
abc
代码段
end sub