变量名称不匹配,但仍然可以使用

时间:2019-03-06 16:42:46

标签: 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 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

足够公平,这是设置输入以备后用。

我的问题是再次调用此子程序:

Call Module3.Inputs(xDOfB, xRetirementAge, xDateRetire, xDOJ, xEmployer, xEmployee, xSalary, _
                         xInflation, xFund, xAVC, xEvalDate, xAVCFund, xCharge, xFund2, xAVCFund2)

z现在是x,这有区别吗?如何运作?

2 个答案:

答案 0 :(得分:1)

如果要在下标之间使用公共变量,则可能要使用公共变量。

Public zDOB As Variant
Public zRetAte As Variant
Sub textSub()
    Call Inputs
End Sub
Sub Inputs()
    zDOB = Range("B1")
    zRetAge = Range("B7")
End Sub

答案 1 :(得分:0)

在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

所以xFoozFoo只是由不同过程(分别是调用代码和Inputs例程)保存的变量


现在,奇怪的是Inputs例程实际上在做什么。它需要大量参数,这些参数通常包含您要在例程中使用的值。但是,它们都立即被覆盖。有点像以下内容:

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

您为什么要这么做?

我怀疑2种可能的原因:

  1. 例程实际上是在声明变量的情况下声明了一组参数,在这种情况下,代码实际上应该是:

     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变量的情况下调用

  2. 该例程正在利用默认的ByRef参数传递,并且实际上旨在覆盖所有这些变量(x被赋为2的原因,即使声明了该变量也是如此)功能之外)

    在这种情况下,应明确签名:

    Sub Inputs(ByRef zDOB As Date, ByRef zRetAge As Double, ByRef zRetDate As Date ...)