Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Using Ranges to input variables to UDF

Hi all,

I would like to use a range of cells in a UDF to input variables. A
trivial example, without using ranges, would be:

______________________________________

Function SimpleSum(s0,s1,s2,s3)

SimpleSum = (s0 + s2 + s3 + s4)

End Function
______________________________________


The input would be, "SimpleSum(A1,A2,A3,A4)", No problem!

However, what if I wanted to use a range. Here is an example just to
illiterate the problem. Why does the following function not work.

_______________________________________

Function SimpleAve(iSpike As Range)

Dim n As Integer
n = iSpike.Count

ReDim s(n)

i = 0
For Each cell In iSpike
s(i) = cell.Value
i = i + 1
Next cell

SimpleAve = (s0 + s1 + s2 + s3)/n

End Function
_________________________________________


I would like the input to be a range, i.e. SimpleAve(A1:A4)

Thanks in advance!
Peter
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Using Ranges to input variables to UDF

s0 < s(0)


Function SimpleAve(iSpike As Range)

Dim n As Integer
n = iSpike.Count

ReDim s(n)

i = 0
For Each cell In iSpike
s(i) = cell.Value
i = i + 1
Next cell

SimpleAve = (s(0) + s(1) + s(2) + s(3))/n

End Function

--
Regards,
Tom Ogilvy

"Peter Bradley" wrote in message
...
Hi all,

I would like to use a range of cells in a UDF to input variables. A
trivial example, without using ranges, would be:

______________________________________

Function SimpleSum(s0,s1,s2,s3)

SimpleSum = (s0 + s2 + s3 + s4)

End Function
______________________________________


The input would be, "SimpleSum(A1,A2,A3,A4)", No problem!

However, what if I wanted to use a range. Here is an example just to
illiterate the problem. Why does the following function not work.

_______________________________________

Function SimpleAve(iSpike As Range)

Dim n As Integer
n = iSpike.Count

ReDim s(n)

i = 0
For Each cell In iSpike
s(i) = cell.Value
i = i + 1
Next cell

SimpleAve = (s0 + s1 + s2 + s3)/n

End Function
_________________________________________


I would like the input to be a range, i.e. SimpleAve(A1:A4)

Thanks in advance!
Peter



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Using Ranges to input variables to UDF

Function SimpleAve(rng As Range)
Dim i As Long, j As Long
Dim n As Long
Dim row As Range
Dim cell As Range
Dim nTotal As Double

If rng.Cells.Count < 0 Then
i = 0
For Each row In rng.Rows
i = i + 1
j = 0
For Each cell In row.Cells
j = j + 1
nTotal = nTotal + cell.Value
If cell.Value < "" Then n = n + 1
Next cell
Next row
End If

SimpleAve = nTotal / n
End Function


--

HTH

RP

"Peter Bradley" wrote in message
...
Hi all,

I would like to use a range of cells in a UDF to input variables. A
trivial example, without using ranges, would be:

______________________________________

Function SimpleSum(s0,s1,s2,s3)

SimpleSum = (s0 + s2 + s3 + s4)

End Function
______________________________________


The input would be, "SimpleSum(A1,A2,A3,A4)", No problem!

However, what if I wanted to use a range. Here is an example just to
illiterate the problem. Why does the following function not work.

_______________________________________

Function SimpleAve(iSpike As Range)

Dim n As Integer
n = iSpike.Count

ReDim s(n)

i = 0
For Each cell In iSpike
s(i) = cell.Value
i = i + 1
Next cell

SimpleAve = (s0 + s1 + s2 + s3)/n

End Function
_________________________________________


I would like the input to be a range, i.e. SimpleAve(A1:A4)

Thanks in advance!
Peter



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 837
Default Using Ranges to input variables to UDF

Previous responders have already covered passing a single range.
To pass an arbitrary number of ranges (as in the worksheet SUM function) see
http://groups.google.com/groups?selm...%40bigfoot.com

Jerry

Peter Bradley wrote:

Hi all,

I would like to use a range of cells in a UDF to input variables. A
trivial example, without using ranges, would be:

______________________________________

Function SimpleSum(s0,s1,s2,s3)

SimpleSum = (s0 + s2 + s3 + s4)

End Function
______________________________________


The input would be, "SimpleSum(A1,A2,A3,A4)", No problem!

However, what if I wanted to use a range. Here is an example just to
illiterate the problem. Why does the following function not work.

_______________________________________

Function SimpleAve(iSpike As Range)

Dim n As Integer
n = iSpike.Count

ReDim s(n)

i = 0
For Each cell In iSpike
s(i) = cell.Value
i = i + 1
Next cell

SimpleAve = (s0 + s1 + s2 + s3)/n

End Function
_________________________________________


I would like the input to be a range, i.e. SimpleAve(A1:A4)

Thanks in advance!
Peter


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
How can I have have one column's input depend on the variables in a second one? willb Excel Worksheet Functions 3 October 18th 06 12:31 PM
Using variables in Chart Ranges [email protected] Charts and Charting in Excel 1 May 9th 06 12:56 AM
Cell Variables and Ranges jcottam Excel Discussion (Misc queries) 2 March 31st 06 07:33 PM
How do I add input data in the input ranges in drop down boxes. oil_driller Excel Discussion (Misc queries) 1 November 9th 05 10:31 PM
using variables to set ranges Dave Marden[_2_] Excel Programming 1 December 2nd 03 01:24 AM


All times are GMT +1. The time now is 12:18 PM.

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"