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

问题描述 投票:0回答:1

你好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但是返回值的方法怎么样?

excel-vba object methods parameter-passing return-value
1个回答
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")
© www.soinside.com 2019 - 2024. All rights reserved.