Excel 变量名不';虽然不匹配,但仍然有效
我正在看一个我工作中的人创建的文件,它有以下内容:Excel 变量名不';虽然不匹配,但仍然有效,excel,vba,Excel,Vba,我正在看一个我工作中的人创建的文件,它有以下内容: Sub Inputs(zDOB As Date, zRetAge As Double, zRetDate As Date, zDOJ As Date, zEmployer As Double, zEmployee As Double, _ zSalary() As Double, zInflation As Double, zFund As Double, zAVCRate As Double, zEvalDate As
Sub Inputs(zDOB As Date, zRetAge As Double, zRetDate As Date, zDOJ As Date, zEmployer As Double, zEmployee As Double, _
zSalary() As Double, zInflation As Double, zFund As Double, zAVCRate As Double, zEvalDate As Date, zAVCFund As Double, _
zCharge As Double, zFund2 As Double, zAVCFund2 As Double)
zDOB = Range("B1")
zRetAge = Range("B7")
zRetDate = Range("B8")
zDOJ = Range("B11")
zEmployer = Range("B15")
zEmployee = Range("B16")
zSalary(0) = Range("B14")
zInflation = Range("B19")
zFund = Range("B20")
zFund2 = Range("B20")
zAVCRate = Range("B24")
zAVCFund = Range("B27")
zAVCFund2 = Range("B27")
zEvalDate = Range("B6")
zCharge = Range("J7")
End Sub
公平地说,这是设置稍后使用的输入
我的问题是,当再次调用此sub时:
Call Module3.Inputs(xDOfB, xRetirementAge, xDateRetire, xDOJ, xEmployer, xEmployee, xSalary, _
xInflation, xFund, xAVC, xEvalDate, xAVCFund, xCharge, xFund2, xAVCFund2)
z现在是x,这有区别吗?这是如何工作的?如果要跨子脚本使用公共变量,则可能需要使用它们
Public zDOB As Variant
Public zRetAte As Variant
Sub textSub()
Call Inputs
End Sub
Sub Inputs()
zDOB = Range("B1")
zRetAge = Range("B7")
End Sub
在VBA中调用函数/子函数时,可以向其传递值,通常称为参数。参数的名称及其类型在例程的签名中定义,如下所示:
Sub Inputs(zDOB As Date, zRetAge As Double, zRetDate As Date, ...)
要使用某些参数调用例程,可以将它们作为原始值从调用代码中传递:
Inputs 1, 2, 4 ... 'equiv to the obsolete Call Inputs(1, 2, 4 ...)
或者,调用例程时提供的值可以存储在变量中。因此,我可以将变量作为参数传递:
Dim x as Date 'here I 'declare' x, you don't generally have to do this but it's advised
x = CDate(Range("A1")) 'here I assign a Value (the date in A1) to a variable (x)
Inputs x, 3, 4, ... 'and call Inputs passing a variable (x) and some values(3,4) as argments
或等效于您的代码:
Dim xDOfB, xRetirementAge, xDateRetire ...
'xDofB = blah 'we could initialise values here
'xRetirementAge = foo
Inputs xDOfB, xRetirementAge, xDateRetire, ... 'call a function with these variables as arguments
因此,xFoo
和zFoo
只是分别由调用代码和输入例程的不同过程持有的变量
现在奇怪的是
输入
例程实际上做了什么。它接受大量参数,这些参数通常包含您希望在例程中使用的值。但是,它们都会立即被覆盖。这有点像下面这样:
Option Explicit
Sub Test()
Dim x As Long
x = 5
Debug.Print x 'prints 5; the value initially stored in x
Foo x 'call Foo, passing the variable x which contains the value 5
Debug.Print x 'Actually prints 2! (see below)
End Sub
Sub Foo(arg1 As Long)
arg1 = 2
Debug.Print arg1 'prints 2, the 5 we passed is gone
End Sub
你为什么要这么做
我怀疑有两个可能的原因:
Sub Inputs()
Dim zDOB As Date
Dim zRetAge As Double
Dim zRetDate As Date
...
zDOB = Range("B1")
zRetAge = Range("B7")
zRetDate = Range("B8")
...
End Sub
并且可以在不传递任何xBlah
变量的情况下调用ByRef
传递参数,事实上设计用于覆盖所有这些变量(为什么x
被指定为2,即使它在函数外部声明)
在这种情况下,签名应明确:
Sub Inputs(ByRef zDOB As Date, ByRef zRetAge As Double, ByRef zRetDate As Date ...)
应该是另一种方式,x变成了z,因为在调用模块时使用了x,模块在第一行子输入(…)中将它们列为z。您应该做的是不传递值,将它们设置为全局变量,只调用SetInput而不传递任何变量,并且可以在两个下标上使用相同的名称。