Excel VBA-调用带有返回变量的参数的方法对象

时间:2018-09-02 21:54:27

标签: excel-vba object methods parameter-passing return-value

VBA for Excel中面向对象编程的新手好,我想知道如何调用通过多个​​参数返回变量的方法

这就是我想要做的

我里面有一个名为 cExcelTable 的类,我有一个方法:

Public Function GetColumnNumberByColumnName(strColumnName As String)
    Dim resultColumnFound As Integer

    '... Here i have the code which find the right column
    '... then i return the result as an integer

    GetColumnNumberByColumnName = resutColumnFound
End Function

然后这是问题所在,如何调用此方法并获取返回值? 我想要的就是这个:

Dim myTable As New cExcelTable

Dim i as Integer

myTable.Workbook = "file.xlsx"
myTable.Sheet = "TestingSheet"
myTable.Table = "TabDatas"
'... And here is my bug that i don't know how to solve
i = myTable.GetColumnNumberByColumnName("TOTAL")

我知道,对于没有返回值的方法,我必须使用 Call ,但是对于返回值的方法呢?

1 个答案:

答案 0 :(得分:0)

已解决

多亏了 PeterT Domenic ,我在一夜熟睡后的评论中发现,我的错误不是方法的调用,而是方法内部两个变量的拼写错误并添加了它应该返回的类型

因此,对于这篇帖子中的新手,答案是 在方法内部,您应该使用此

'Add the type the method it should return "As Integer"
Public Function nameOfTheMethod(parameterNumber1 As String) As Integer
    Dim resultColumnFound As Integer

    '... type your code
    '... then return the result with the correct spelling

    GetColumnNumberByColumnName = resultColumnFound
End Function

然后在代码中,您可以像这样调用您的方法

Dim myObject As New cMyClass 'cMyClass  is the name of your class and myObject is the name of your object you are creating
Dim i as Integer

i = myObject.nameOfTheMethod("test")