Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 99
Default How to use multiple sheets with range in userdefined function.

Hi to all,

I want to have a userdefined function which accepts a range over more
then one sheet.
eg: MyFunction(Sheet1:Sheet3!A1:B10)

This is code

Public Function MyFunction(ByVal MyArea As ????) As Long
Dim rngCell As Range
Dim lngResult As Long
'
Application.Volatile
'
On Local Error GoTo MyFunction_err

For Each rngCell In MyArea
' Do this and that to calculate lngResult
Next rngCel

MyFunction = lngResult
GoTo CountUniqueValues_exit
CountUniqueValues_err:
MsgBox Err.Description, , Err.Number
MyFunction = -1
CountUniqueValues_exit:
Set UniqueValues = Nothing
End Function

With the standard function SUM you can do this.

I have tried these definitions:
MyFunction(ByVal MyArea As Range) = #VALUE
MyFunction(ByVal MyArea As Variant) = Error 424 / Error 2015 on
MyArea
MyFunction(ByVal MyArea As Object) = #VALUE

Any ideas?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 575
Default How to use multiple sheets with range in userdefined function.

Try using a Paramarray. This function doesn't really do anything other than
count numeric cells, but it shows how to take an array of range values. You
would use something like = CountNumericCells(Sheet1!a1,Sheet2!A3:Z11)

Public Function CountNumericCells(ParamArray CellRange() As Variant) As
Variant
Dim SubRange As Variant, LocalRange as Range, Cell as Range
Dim lCount as long
For Each SubRange In CellRange
Set S = Sheets(SubRange.Parent.Name)
Set LocalRange = S.Range(SubRange.Address)
Set LocalRange = Intersect(LocalRange, S.UsedRange)
lCount = lCount + LocalRange.Count
For Each Cell In LocalRange
If Not (IsEmpty(Cell.Value)) And IsNumeric(Cell.Value) Then
lCount = lCount +1
Next Cell
Next SubRange
CountNumericCells = lCount
End Sub


Robin Hammond
www.enhanceddatasystems.com

"Wouter HM" wrote in message
om...
Hi to all,

I want to have a userdefined function which accepts a range over more
then one sheet.
eg: MyFunction(Sheet1:Sheet3!A1:B10)

This is code

Public Function MyFunction(ByVal MyArea As ????) As Long
Dim rngCell As Range
Dim lngResult As Long
'
Application.Volatile
'
On Local Error GoTo MyFunction_err

For Each rngCell In MyArea
' Do this and that to calculate lngResult
Next rngCel

MyFunction = lngResult
GoTo CountUniqueValues_exit
CountUniqueValues_err:
MsgBox Err.Description, , Err.Number
MyFunction = -1
CountUniqueValues_exit:
Set UniqueValues = Nothing
End Function

With the standard function SUM you can do this.

I have tried these definitions:
MyFunction(ByVal MyArea As Range) = #VALUE
MyFunction(ByVal MyArea As Variant) = Error 424 / Error 2015 on
MyArea
MyFunction(ByVal MyArea As Object) = #VALUE

Any ideas?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default How to use multiple sheets with range in userdefined function.

Hi Wouter,

There is no satisfactory answer using VBA AFAIK: the fundamental problem is
that the Range object does not allow for 3-dimensional cells.

You can get round it if you are willing to use different syntax for a 3-d
range which separates the first sheet and last sheet into separate
arguments, but its not user-friendly having to invent new excel syntax.


--
Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com/FxlV2WhatsNew.htm

"Wouter HM" wrote in message
om...
Hi to all,

I want to have a userdefined function which accepts a range over more
then one sheet.
eg: MyFunction(Sheet1:Sheet3!A1:B10)

This is code

Public Function MyFunction(ByVal MyArea As ????) As Long
Dim rngCell As Range
Dim lngResult As Long
'
Application.Volatile
'
On Local Error GoTo MyFunction_err

For Each rngCell In MyArea
' Do this and that to calculate lngResult
Next rngCel

MyFunction = lngResult
GoTo CountUniqueValues_exit
CountUniqueValues_err:
MsgBox Err.Description, , Err.Number
MyFunction = -1
CountUniqueValues_exit:
Set UniqueValues = Nothing
End Function

With the standard function SUM you can do this.

I have tried these definitions:
MyFunction(ByVal MyArea As Range) = #VALUE
MyFunction(ByVal MyArea As Variant) = Error 424 / Error 2015 on
MyArea
MyFunction(ByVal MyArea As Object) = #VALUE

Any ideas?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to use multiple sheets with range in userdefined function.

Check out Mr. Erlandsen's site for sample code:
http://www.erlandsendata.no/english/vba/adodao/

--
Regards,
Tom Ogilvy

"Wouter HM" wrote in message
om...
Hi to all,

I want to have a userdefined function which accepts a range over more
then one sheet.
eg: MyFunction(Sheet1:Sheet3!A1:B10)

This is code

Public Function MyFunction(ByVal MyArea As ????) As Long
Dim rngCell As Range
Dim lngResult As Long
'
Application.Volatile
'
On Local Error GoTo MyFunction_err

For Each rngCell In MyArea
' Do this and that to calculate lngResult
Next rngCel

MyFunction = lngResult
GoTo CountUniqueValues_exit
CountUniqueValues_err:
MsgBox Err.Description, , Err.Number
MyFunction = -1
CountUniqueValues_exit:
Set UniqueValues = Nothing
End Function

With the standard function SUM you can do this.

I have tried these definitions:
MyFunction(ByVal MyArea As Range) = #VALUE
MyFunction(ByVal MyArea As Variant) = Error 424 / Error 2015 on
MyArea
MyFunction(ByVal MyArea As Object) = #VALUE

Any ideas?



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to use multiple sheets with range in userdefined function.

Sorry, replied to the wrong post.

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote in message
...
Check out Mr. Erlandsen's site for sample code:
http://www.erlandsendata.no/english/vba/adodao/

--
Regards,
Tom Ogilvy

"Wouter HM" wrote in message
om...
Hi to all,

I want to have a userdefined function which accepts a range over more
then one sheet.
eg: MyFunction(Sheet1:Sheet3!A1:B10)

This is code

Public Function MyFunction(ByVal MyArea As ????) As Long
Dim rngCell As Range
Dim lngResult As Long
'
Application.Volatile
'
On Local Error GoTo MyFunction_err

For Each rngCell In MyArea
' Do this and that to calculate lngResult
Next rngCel

MyFunction = lngResult
GoTo CountUniqueValues_exit
CountUniqueValues_err:
MsgBox Err.Description, , Err.Number
MyFunction = -1
CountUniqueValues_exit:
Set UniqueValues = Nothing
End Function

With the standard function SUM you can do this.

I have tried these definitions:
MyFunction(ByVal MyArea As Range) = #VALUE
MyFunction(ByVal MyArea As Variant) = Error 424 / Error 2015 on
MyArea
MyFunction(ByVal MyArea As Object) = #VALUE

Any ideas?







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default How to use multiple sheets with range in userdefined function.

AFAIK, there is any way to make that work. 3D worksheet references are
not part of the XL object model or the UDF model.

If you install Laurent Longre's Morefunc utility
(http://longre.free.fr/english/), you can then use the ThreeD function
to convert 3D references into a matrix and use that in your function.
[Note that I haven't had any reason to use the ThreeD function in
conjunction with a UDF.]

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Hi to all,

I want to have a userdefined function which accepts a range over more
then one sheet.
eg: MyFunction(Sheet1:Sheet3!A1:B10)

This is code

Public Function MyFunction(ByVal MyArea As ????) As Long
Dim rngCell As Range
Dim lngResult As Long
'
Application.Volatile
'
On Local Error GoTo MyFunction_err

For Each rngCell In MyArea
' Do this and that to calculate lngResult
Next rngCel

MyFunction = lngResult
GoTo CountUniqueValues_exit
CountUniqueValues_err:
MsgBox Err.Description, , Err.Number
MyFunction = -1
CountUniqueValues_exit:
Set UniqueValues = Nothing
End Function

With the standard function SUM you can do this.

I have tried these definitions:
MyFunction(ByVal MyArea As Range) = #VALUE
MyFunction(ByVal MyArea As Variant) = Error 424 / Error 2015 on
MyArea
MyFunction(ByVal MyArea As Object) = #VALUE

Any ideas?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to use multiple sheets with range in userdefined function.

Thank goodness. I was confused<g

Bob

"Tom Ogilvy" wrote in message
...
Sorry, replied to the wrong post.

--
Regards,
Tom Ogilvy




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
same range, multiple sheets, different sorts, help please! nikmasteed Excel Worksheet Functions 2 May 19th 06 04:30 PM
Want to use a range name on multiple sheets Shelter Pat Excel Discussion (Misc queries) 1 April 13th 06 03:30 PM
Sum a range across multiple sheets CarlosAntenna Excel Worksheet Functions 4 October 29th 04 12:22 PM
Changing one range on multiple sheets. Mike-hime Excel Programming 3 January 6th 04 10:09 PM
UserDefined Function that opens another w/book Regan Excel Programming 1 September 1st 03 10:21 AM


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