#1   Report Post  
Posted to microsoft.public.excel.misc
SPeterson
 
Posts: n/a
Default Custom Function

I am creating a custom function and want one of my arguments to be a range.
For example, the sum function accepts a range argument (=Sum(range)). How
does the Sum function define that range argument? Fill in the blank for me
and it should help me with my problem.

Function Sum(???)

Sum = ???

End Function
  #2   Report Post  
Posted to microsoft.public.excel.misc
Gary''s Student
 
Posts: n/a
Default Custom Function

Enter as :

Function zum(r As Range) As Variant
Dim rr As Range
zum = 0#
For Each rr In r
zum = zum + rr.Value
Next
End Function


use as:

=zum(B1:B6)
--
Gary''s Student


"SPeterson" wrote:

I am creating a custom function and want one of my arguments to be a range.
For example, the sum function accepts a range argument (=Sum(range)). How
does the Sum function define that range argument? Fill in the blank for me
and it should help me with my problem.

Function Sum(???)

Sum = ???

End Function

  #3   Report Post  
Posted to microsoft.public.excel.misc
JAdamJ
 
Posts: n/a
Default Custom Function

Function mySum(myRange As Range)
mySum = 0

For Each c In myRange
mySum = mySum + c.Value
Next

End Function


"SPeterson" wrote:

I am creating a custom function and want one of my arguments to be a range.
For example, the sum function accepts a range argument (=Sum(range)). How
does the Sum function define that range argument? Fill in the blank for me
and it should help me with my problem.

Function Sum(???)

Sum = ???

End Function

  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Custom Function

And if you're passing a bunch of stuff (strings, arrays, ranges), you have to
code that yourself.

For example:

Option Explicit
Function mySum(ParamArray myParms()) As Double

Dim myCell As Range
Dim myElement As Variant
Dim tempSum As Double
Dim iCtr As Long

tempSum = 0

For Each myElement In myParms
If TypeOf myElement Is Range Then
For Each myCell In myElement.Cells
If Application.IsNumber(myCell.Value) Then
tempSum = tempSum + myCell.Value
Else
'skip it
End If
Next myCell
ElseIf IsArray(myElement) Then
For iCtr = LBound(myElement) To UBound(myElement)
If Application.IsNumber(myElement(iCtr)) Then
tempSum = tempSum + myElement(iCtr)
End If
Next iCtr
ElseIf Application.IsNumber(myElement) Then
tempSum = tempSum + myElement
End If
Next myElement

mySum = tempSum

End Function

Tested with:

Sub testme()
Dim myArr As Variant
myArr = Array("a", "b", 9999)
MsgBox mySum(ActiveSheet.Range("a1:a10"), "a", "b", 999, myArr)
End Sub

===
Kind of like if you did:
=sum(a1:a10,"a","b",999)



SPeterson wrote:

I am creating a custom function and want one of my arguments to be a range.
For example, the sum function accepts a range argument (=Sum(range)). How
does the Sum function define that range argument? Fill in the blank for me
and it should help me with my problem.

Function Sum(???)

Sum = ???

End Function


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom function returning VALUE error alex.k Excel Discussion (Misc queries) 6 September 27th 05 03:05 AM
creating function (vba) with range arguments Fredouille Excel Worksheet Functions 2 September 12th 05 11:01 AM
Force refresh of custom functions donesquire Excel Worksheet Functions 5 May 11th 05 07:36 PM
Conversion SVC Excel Worksheet Functions 9 February 28th 05 02:29 PM
HOW CAN I GET OFFICE 2003 EXCEL BASIC TO NEST FUNCTIONS LIKE EXCE. Robert AS Excel Worksheet Functions 4 December 2nd 04 10:49 AM


All times are GMT +1. The time now is 11:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"