Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3665|回复: 5

[已解决]Function的参数太多怎么办

[复制链接]
发表于 2010-3-25 09:42 | 显示全部楼层 |阅读模式

07对SUM函数的参数,允许最多255个。如果自己写SUM的Function。请问:

真要写255个参数吗?求和时,只能一个个相加,没有更好方法么?

谢谢!

最佳答案
2010-3-25 11:22

传入可变数组

 


Function Test(ParamArray vArr() As Variant) As Double
Dim cArr As Variant
Dim c As Variant
Dim tmp As Double
For Each cArr In vArr
    If IsArray(cArr) Then
        For Each c In cArr
            tmp = tmp + c
        Next
    Else
        tmp = tmp + cArr
    End If
Next
Test = tmp
End Function

Sub ff()
Dim arr
MsgBox Test(1, 2, Range("a1:a4"), Array(1, 2, 3))
MsgBox Test(1, 2, 3, 4, 5, 6, 1000)
End Sub


发表于 2010-3-25 11:22 | 显示全部楼层    本楼为最佳答案   

传入可变数组

 


Function Test(ParamArray vArr() As Variant) As Double
Dim cArr As Variant
Dim c As Variant
Dim tmp As Double
For Each cArr In vArr
    If IsArray(cArr) Then
        For Each c In cArr
            tmp = tmp + c
        Next
    Else
        tmp = tmp + cArr
    End If
Next
Test = tmp
End Function

Sub ff()
Dim arr
MsgBox Test(1, 2, Range("a1:a4"), Array(1, 2, 3))
MsgBox Test(1, 2, 3, 4, 5, 6, 1000)
End Sub


回复

使用道具 举报

 楼主| 发表于 2010-3-25 11:58 | 显示全部楼层
回复

使用道具 举报

发表于 2010-3-25 20:09 | 显示全部楼层

好![em17]
回复

使用道具 举报

发表于 2010-3-26 06:02 | 显示全部楼层

学习[em02][em02]
回复

使用道具 举报

发表于 2013-5-4 20:30 | 显示全部楼层
学习吕布。。。。高。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-30 08:28 , Processed in 0.290652 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表