简体   繁体   English

Excel VBA更改传递给函数的参数

[英]Excel vba change arguments passed to function

Say I have a function that takes two Date arguments. 说我有一个带有两个Date参数的函数。 Then I need to change one of those (say, I need to change its month). 然后,我需要更改其中之一(例如,我需要更改其月份)。 Is the only way to do it to reassign it to variables and then change or can it be changed by referencing to it by its argument name? 是将它重新分配给变量然后进行更改的唯一方法,还是可以通过其参数名称引用它来更改它?

Function test(date1 As Date, date2 As Date, change As Boolean)
   If change=True Then
         date1 = DateSerial(year(date1), 10, day(date1))   'would it change date1's month or not?
   End If

   test=date1
End Function

It's generally considered a best practice not to change inputs, although there is nothing preventing your from doing so - those arguments are treated just like variables inside the procedure. 尽管没有什么阻止您这样做的,但是通常也认为不更改输入是一种最佳做法-这些参数被视为过程中的变量一样对待。 For short, simple functions you can get away with it. 简而言之,简单的功能就可以解决。 But if you have a longer, more complex function and you stick to never changing inputs, your code becomes much easier to maintain. 但是,如果您拥有更长,更复杂的功能,并且坚持不更改输入,那么代码将变得更加易于维护。

Say you get a date from the user, but you want to go back to the last statement date which is always the last day of the previous month. 假设您从用户那里获得了一个日期,但是您想回到最后一个对帐单日期,该日期始终是上个月的最后一天。 You can manipulate the argument like 您可以像这样操纵参数

Public Function LastStatementAmount(ByVal inputDate As Date) As Double

    Dim Return_ As Double

    inputDate = DateSerial(Year(inputDate), Month(inputDate), 0)

    'do calculations here

    LastStatementAmount = Return_

End Function

and that works. 那行得通。 But the better way is to change the variable name to reflect what changes you made to the value 但是更好的方法是更改​​变量名称以反映对值所做的更改

Public Function LastStatementAmount(ByVal inputDate As Date) As Double

    Dim Return_ As Double
    Dim stmtDate As Date

    stmtDate = DateSerial(Year(inputDate), Month(inputDate), 0)

    'do calculations here

    LastStatementAmount = Return_

End Function

If this were a larger function, it would be better to see stmtDate and know that you had already modified the input date. 如果这是一个较大的函数,则最好查看stmtDate并知道您已经修改了输入日期。

Another reason not to change an input parameter, as mentioned in the comments, is that if you don't specify a passing mechanism, the parameter is, by default, ByRef and if the calling procedure is expecting that the value didn't change, you might break the code. 如注释中所述,不更改输入参数的另一个原因是,如果您未指定传递机制,则默认情况下该参数为ByRef,并且如果调用过程期望该值不变,您可能会破坏代码。 You should always specify ByVal or ByRef so it's clear what your code intends. 您应该始终指定ByVal或ByRef,以便清楚您的代码意图。

Within the code you can use Date1 and Date2 as variables, so you can alter their values. 在代码中,可以将Date1和Date2用作变量,因此可以更改它们的值。 The variables Date1 and Date2 are declared within the function statement, and so their scope is purely withing the function. 变量Date1和Date2在函数语句中声明,因此它们的作用域与函数完全无关。 So worries abut changing the inputs are misguided in the contect of a UDF - Date1 and Date2 do not exist anywhere else. 因此,担心更改输入的错误被UDF所误导-Date1和Date2在其他任何地方都不存在。 If you were using this in another routine however, then you could indeed risk this, and so you'd declare them By Value 但是,如果您在另一个例程中使用它,那么您确实可能会冒险,因此您可以按值声明它们

Function Test(ByVal Date1 As Date, ByVal Date2 As Date, Change As Boolean) 函数测试(ByVal Date1作为日期,ByVal Date2作为日期,更改为布尔值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM