Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过VBA中的函数传递数组或范围_Vba_Excel - Fatal编程技术网

通过VBA中的函数传递数组或范围

通过VBA中的函数传递数组或范围,vba,excel,Vba,Excel,所以我想做一个基本函数,它取我在Excel中突出显示的值的平均值。我很清楚Excel中已经有一个内置函数用于此操作,但我正在尝试将其作为实践 Function averagetest(range As Range) '<------(Is this how I pass a Range into a function?) Dim N as Integer Dim i as Integer Dim average as Double avera

所以我想做一个基本函数,它取我在Excel中突出显示的值的平均值。我很清楚Excel中已经有一个内置函数用于此操作,但我正在尝试将其作为实践

Function averagetest(range As Range) '<------(Is this how I pass a Range into a function?)
      Dim N as Integer
      Dim i as Integer
      Dim average as Double
      average = 0 
      N = LengthofRange '<--------- (Is there a way to get the length of the 
      range like UBound or LBound for an array?)
      Do Until i = LengthofRange
          average = average + Range(i, i+1) '<--------(Is this how you call a 
          specific element in the range? I'm just adding every element in the 
          Range)
          i = i + 1
      Loop
average = average/N

End Function 
我的问题是我不确定如何通过一个范围,然后调用范围中的特定元素

下面是我一直在玩的伪代码。我知道它可能写得很糟糕。我是初学者,我只是想练习一下

Function averagetest(range As Range) '<------(Is this how I pass a Range into a function?)
      Dim N as Integer
      Dim i as Integer
      Dim average as Double
      average = 0 
      N = LengthofRange '<--------- (Is there a way to get the length of the 
      range like UBound or LBound for an array?)
      Do Until i = LengthofRange
          average = average + Range(i, i+1) '<--------(Is this how you call a 
          specific element in the range? I'm just adding every element in the 
          Range)
          i = i + 1
      Loop
average = average/N

End Function 

Function averagetest(范围为范围)不要将
range
用作变量

然后可以使用rows.Count或Columns.Count来获取扩展数据块

Function averagetest(rng As Range)
      Dim N as Integer
      Dim i as Integer
      Dim average as Double
      average = 0 
      N = rng.rows.count 
      For  i = 1 to N 'use For loop
          average = average + rng.cells(i,1)'Cells will work here
      Next i
      averagetest= average/N

End Function 
或者您可以这样做--实际上不需要迭代单元格计数,只需迭代
rng.cells
集合中的
每个
单元格即可。我还将变量名从
average
(这是误导性的)更改为更具描述性的名称,如
total

Option Explicit
Function averagetest(rng As Range)
    Dim cl As Range
    Dim total As Double

    For Each cl In rng.Cells
        total = total + cl.Value
    Next
    averagetest = total / rng.Cells.Count

End Function
作为奖励,后一种方法也适用于二维范围


请注意,这会将空单元格视为0值(工作表函数的
AVERAGE
会忽略空单元格,因此您的结果可能会有所不同),如果该范围内有非数值,则会引发错误。

您不能假设
范围
是连续的,您也不能假设
范围将是水平的,也不能是垂直的

Range
是对象的集合,因此可以对每个
循环使用
进行迭代,以获得最佳性能

假设该函数将用作UDF工作表函数,因此在标准模块(.bas)中定义:

注:

  • 参数通过val传递,并且不是以现有类型(
    范围
    )命名的;我们不需要对范围指针的引用,它的副本就足够了
  • 函数显式地
    Public
    ,并具有显式的返回类型(
    Variant
  • 函数返回一个
    变量
    ,以便在“快乐路径”中返回一个
    双精度
    结果,或者在适用时返回一个适当的
    错误
    值(
    #Div/0!
  • 函数仅计算数值单元格,这意味着即使
    目标
    范围包含错误值,它也可以工作。注释掉的代码将退出并返回一个
    #值遇到非数值时出错

如何“通过范围”是来电者的问题。有很多方法可以做到这一点-从Excel公式:

=AverageTest(A1:A10)
=AverageTest(A1:B12,F4:L4)
也可以在VBA代码中使用它:

foo = Module1.AverageTest(ActiveSheet.Range("A1:D10"))