Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default How to write a range function?

Hi,

How can I write a function (such as sum) which could be called on the
result of range operation such as A1:A10/A1:A10.

In other words, I want to call function in the following way:

mysum(A1:A10/A1:A10) Ctrl-Shift-Enter

The following does not work:

Function mysum(r As Range) As Double
Dim i As Integer
pgSum = 0
For i = 1 To r.Rows.Count
pgSum = pgSum + r(i)
Next i
End Function

Many thanks in advance!

Aaron Fude

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How to write a range function?

Hi Aaron,

Try:

'=============
Public Function mySum(r As Range) As Double
Dim i As Integer

For i = 1 To r.Rows.Count
If IsNumeric(r(i)) Then
mySum = mySum + r(i)
End If
Next i
End Function
'<<=============


---
Regards,
Norman



wrote in message
ups.com...
Hi,

How can I write a function (such as sum) which could be called on the
result of range operation such as A1:A10/A1:A10.

In other words, I want to call function in the following way:

mysum(A1:A10/A1:A10) Ctrl-Shift-Enter

The following does not work:

Function mysum(r As Range) As Double
Dim i As Integer
pgSum = 0
For i = 1 To r.Rows.Count
pgSum = pgSum + r(i)
Next i
End Function

Many thanks in advance!

Aaron Fude



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default How to write a range function?

Hi Aaron,

I would additionally suggest that you change:

Dim i As Integer


to

Dim i As Long


---
Regards,
Norman


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default How to write a range function?

Try:

Function mysum(ByVal r As Variant) As Variant
Dim tmp As Variant
Dim i As Long, j As Long
Dim pgsum As Variant
tmp = r
For i = LBound(tmp, 1) To UBound(tmp, 1)
For j = LBound(tmp, 2) To UBound(tmp, 2)
pgsum = pgsum + tmp(i, j)
Next
Next
mysum = pgsum
End Function

keizi

wrote in message
ups.com...
Hi,

How can I write a function (such as sum) which could be called on the
result of range operation such as A1:A10/A1:A10.

In other words, I want to call function in the following way:

mysum(A1:A10/A1:A10) Ctrl-Shift-Enter

The following does not work:

Function mysum(r As Range) As Double
Dim i As Integer
pgSum = 0
For i = 1 To r.Rows.Count
pgSum = pgSum + r(i)
Next i
End Function

Many thanks in advance!

Aaron Fude


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 to write (Selected range - 1) Damian Excel Discussion (Misc queries) 2 September 4th 09 08:20 PM
how do you write format results of a function within a function? sangee Excel Worksheet Functions 3 June 14th 07 12:45 AM
write a formula to a range Martin Excel Programming 1 January 29th 06 10:37 AM
how do I write a vlookup function within an iserror function so t. JBLeeds Excel Worksheet Functions 2 March 16th 05 10:30 AM
Show one range in a combobox and write the 2nd range! Kevin Excel Programming 1 October 17th 03 05:52 AM


All times are GMT +1. The time now is 12:17 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"