Excel vba - 将工作表传递给公共函数

时间:2017-09-05 11:26:32

标签: excel vba excel-vba

我有公共职能:

Public Function Test(wrs As Worksheet, arr As Variant) As Variant

我有主要分:

Sub Main()
Dim ws as Worksheet
Dim out, in

ws = ThisWorkbook.Sheets("Sheet1") 

out = Test (ws, in)

调用测试我得到“ByRef参数类型不匹配”错误。

1 个答案:

答案 0 :(得分:6)

使用Set关键字,如下所示:

set ws = ThisWorkbook.Sheets("Sheet1") 

修改:

然后确保您也设置了函数的返回值。像这样:

Public Function Test() As Variant
    Set Test = ActiveSheet
End Function

Public Sub TestMe()
    Debug.Print Test.name
End Sub