将变量作为参数传递给VBA中的函数 [英] Passing a variant as parameter to a function in VBA

查看:1050
本文介绍了将变量作为参数传递给VBA中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数

Function convertToDict(arrayIp As Variant) As Object
Dim dict As Object
Set dict = CreateObject("scripting.dictionary")
For Each element In arrayIp

    If dict.exists(element) Then
        dict.Item(element) = dict.Item(element) + 1
    Else
        dict.Add element, 1
    End If
Next
End Function

我试图从sub

Dim dict As Object
varray = Range("B4:B" & finalRow - 1).Value
dict = convertToDict(varray)

但它会抛出错误:


运行时错误450,错误的数量参数或无效属性

Run time error 450, wrong number of arguments or invalid property

我在这里做的错误是什么?

what is the mistake that I have done here?

我传递一个变体,结果是一个 Object

I am passing a variant and result is an Object.

推荐答案

对象 ,您需要在功能和子文件中设置

Function convertToDict(arrayIp As Variant) As Object
    Dim dict As Object
    Set dict = CreateObject("scripting.dictionary")
    For Each element In arrayIp

        If dict.exists(element) Then
            dict.Item(element) = dict.Item(element) + 1
        Else
            dict.Add element, 1
        End If
    Next
    Set convertToDict = dict
End Function

Sub qwerty()
    Dim dict As Object
    finalRow = 10
    varray = Range("B4:B" & finalRow - 1).Value
    Set dict = convertToDict(varray)
End Sub

这篇关于将变量作为参数传递给VBA中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆