将一系列单元格从excel传递到vba函数中

我试图创build一个函数,以便= processCells(A1:A10)将采取单元格的范围,并允许我将10添加到每个项目,并显示在单元格A11:A20新的数字。 我想在工作表中使用该function,以便用户可以手动select单元格A1:A10,因此他们可以selectB1:B10等…而不是A1:A10

我的问题是将工作表单元格的范围传递给一个函数,然后处理它们。

这里有一个示例UDF让你开始

Function processCells(rng As Variant, Optional AddValue = 10) As Variant Dim v As Variant Dim i As Long, j As Long Select Case TypeName(Application.Caller) Case "Range" ' Called from a Formula v = rng If IsArray(v) Then ' Called from an Array Formula For j = 1 To UBound(v, 1) For i = 1 To UBound(v, 2) If IsNumeric(v(j, i)) Then v(j, i) = v(j, i) + AddValue End If Next i, j Else ' Called from a single Cell If IsNumeric(v) Then v = v + AddValue End If End If processCells = v Case Else processCells = vbEmpty End Select End Function 

按照您的描述使用它,在单元格单元格A11:A20中将其input为Array Formula =processCells(A1:A10)