VBA 定义能返回数组公式的自定义函数

本文介绍了如何在VBA中定义两个自定义函数,分别用于返回基于参数范围动态大小的数组和返回固定大小的数组。第一个函数根据输入范围的值计算乘以100的结果,第二个函数接收起始和结束时间,返回它们之间的时间差(小时、分钟、秒)。这两个函数都需要以数组公式的形式在Excel中输入。
摘要由CSDN通过智能技术生成

返回一个变量大小结果数组的方法

此方法返回基于一个参数范围的值的数组。结果数组的大小具体取决于参数数组中的元素数量波动。例如对于假定您要创建一个范围中的每个值乘以 100 的函数。下面的自定义函数接受作为其参数的数组 (某一范围的值):

      Function Multiply_Range(myrange As Object) As Variant
      Dim temp As Variant
      Dim i As Integer, j As Integer
      temp = myrange.Value 'creates a copy of the values in myrange
      ' if more than one element then loop through both dimensions of
      ' the array and multiply each element by 100.
      ' if not more than one element then temp is multiplied by 100.
      If IsArray(temp) Then
         For i = 1 To UBound(temp, 1)
            For j = 1 To UBound(temp, 2)
               temp(i, j) = temp(i, j) * 100
            Next j
         Next i
      Else
         temp = temp * 100
      End If
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值