Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Custom function returning VALUE error | Excel Discussion (Misc queries) | |||
creating function (vba) with range arguments | Excel Worksheet Functions | |||
Force refresh of custom functions | Excel Worksheet Functions | |||
Conversion | Excel Worksheet Functions | |||
HOW CAN I GET OFFICE 2003 EXCEL BASIC TO NEST FUNCTIONS LIKE EXCE. | Excel Worksheet Functions |