具有多个参数的功能 [英] Function with multiple parameters

查看:75
本文介绍了具有多个参数的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个参数的VBA函数,我需要从excel工作表而不是从子函数获取,如何将输入划分为函数所需的参数数量?

I have a VBA function with multiple arguments, which I need to get from the excel sheet and not from a sub, how do I divide the inputs into the number of parameters i need for my function?

例如:

Function func(a as double,b as double) as double
    'calculations
    'some return value
End Function

这就是我一直试图获取值的方式:

This is how I have been trying to get the values:

推荐答案

如果要处理多个其数量未知的参数,请使用ParamArray参数

if you want to handle multiple arguments of which you don't know the number of, then use ParamArray argument

假设func()应该简单地将您传递的参数相加:

for instance, assuming func() should simply sum the arguments you pass it:

Function func(ParamArray args() As Variant) As Double
    Dim i As Long
    Dim cell As Range

    For i = LBound(args) To UBound(args) '<--| loop through each passed argument
        If TypeName(args(i)) = "Range" Then '<--| if the current element is a Range
            For Each cell In args(i) '<--| loop through range cells
                func = func + cell.Value
            Next cell
        Else '<--| otherwise
            func = func + args(i) '<--| simply process the current argument value
        End If
    Next i
End Function

这篇关于具有多个参数的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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