1

我对在 excel 中编写函数很陌生(主要是编写子程序)。

我想知道下面的函数声明是什么意思?

public function function_name(args as string) as string

我了解直到第二个实例的所有内容as string。这对我来说是新事物,我不确定这与仅仅声明有什么不同:

public function function_name(args as string)

4

4 回答 4

3

public function function_name(args as string) as string

  • public is the access definition. public means accessible across the VBA Project

  • function means that is it a function (meaning it is supposed to return something)

  • function_name is the name of a function (can't start with 1 or underscore)

  • args is the local parameter name to be used within the body of function

  • args as String indicates that the function is expecting the args to be of a String type

  • ...) As String indicates that the function will be returning a String data type. So if you have had dimensioned a String type variable you would be able to assign a value to it using the function.


the standard (default) declaration without explicitly specifying the type to be returned returns a Variant

It's the same as declaring a variable without specifying its type.

Dim aVariable

with is equivalent to

Dim aVariable as Variant

because Variant is the default type.

so the as Variant always exist unless there is a different type specified. And because it's default you do not have to explicitly code it.

It's somehow similar to Range("A1").Value and Range("A1") - both are the same because .Value is the default property of a Range object.

What happens now is the compiler evaluates what value goes into the aVariable under the hood and assigns that type to the variable.

Let's say you have

Sub Main()

    Dim varVariable
    Dim strVariable As String

    varVariable = "hello world"
    strVariable = "hello world"

    MsgBox "varVariable is of " & TypeName(varVariable) & " type" & vbCrLf & _
           "strVariable is of " & TypeName(strVariable) & " type"

End Sub

Like I've said now both are of String type

enter image description here


Not sure how familiar with for example C# you are but in C# you declare the return type of a function right after the access modifier ie.

public string myFunction(string args)

so in VB/VBA the second as String is equal to the first string (right after public) in C#

in C# you would use a return keyword while in VBA you replace the return keyword with the function name. Therefore in VBA a very basic sample

Public Function ReturnFirst3Characters(args As String) As String
    ReturnFirst3Characters = IIf(Len(args) > 2, Left(args, 3), args)
End Function

Function returns first 3 characters of the string you have passed to it (if the string is longer then 3 characters, if not it returns the string you passed to the function)

于 2013-12-18T23:02:50.040 回答
0

如果您熟悉Procedures,请将 aFunction视为Procedure返回值的 a。as Stringas end表示返回什么类型值。

返回值的方法是使用函数的名称,就好像它是一个变量一样:

Public Function ConvertToUpperCase(str as string) as string
    ConvertToUpperCase = UCase(str)
End Function
于 2013-12-18T23:27:00.570 回答
0

来自http://www.cpearson.com/excel/writingfunctionsinvba.aspx

Function RectangleArea(Height As Double, Width As Double) As Double
    RectangleArea = Height * Width
End Function

此函数将两个 Double 类型变量 Height 和 Width 作为输入,并返回一个 Double 作为其结果。

于 2013-12-18T22:21:05.400 回答
0

第二个“as”表示函数返回的类型。该函数返回一个字符串,您可以通过以下方式在函数内部返回一个值:

function_name="Value"

于 2013-12-18T22:17:24.887 回答