function that takes range as an argument -vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi I created vba function that takes 3 numbers and add them and then multiple by 1.13. Everything fine if I say

= foo(a1,a2,a3)

but I got #Value if I type

= foo(A1:A3)

Why is that? Thank you.
+++

Function foo(x As Integer, y As Integer, z As Integer) As Double
foo = (x + y + z) * 1.13
End Function
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
If you want to add a range instead of individual number arguments, you could write it like this.

Code:
Function PointOneThree(r As Range)
PointOneThree = Application.WorksheetFunction.Sum(r) * 1.13
End Function
 
Upvote 0
A1:A3 is a range object not a series of three integers. You could do it like this:

Function foo(R as range) as double
foo = Application.Sum(R)*1.13
End Function

Then use it as =foo(A1:A3)
 
Upvote 0
=foo(A1:A3) is passing one argument (the range A1:A3) rather than the required 3.

Try using a paramarray.
Code:
Function Foo2(ParamArray aterm() As Variant) As Double
    Dim i As Long
    Dim oneSubTerm As Variant
    
    For i = 0 To UBound(aterm)
        Select Case TypeName(aterm(i))
            Case "Range"
                For Each oneSubTerm In aterm(i).Cells
                    Foo2 = Foo2 + Val(CStr(oneSubTerm.Value))
                Next oneSubTerm
            Case "Double", "Single", "Integer", "Byte", "Long"
                Foo2 = Foo2 + aterm(i)
            Case Else
            
        End Select
        
    Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top