I'm trying to create a function so that =processCells(A1:A10) will take the range of cells and allow me to add 10 to each item and display the new numbers in cells A11:A20. I want to use the function in the worksheet so the user can select the cells A1:A10 manually so therefore they could select B1:B10 etc... instead of A1:A10
My problem is passing the range of cells for a worksheet to a function and then processing them.
Here's a sample UDF to get you started
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
To use it as you describe, enter it as an Array Formula =processCells(A1:A10)
in cells cells A11:A20
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.