Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Retrun a worksheet from a function

Hi,
My problem is rather basic but I am stumped.
Here it is: is it possible to return a worksheet object from a user defined
function (possibly via a ByRef argument)?
I'd appreciate an exemple of syntax in order to try it.
Many thanks in advance.
--
Jean-Pierre Bidon
Interstat
91 rue de Rennes
75006 Paris
Tél: 01 45 49 19 17


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Retrun a worksheet from a function

Here is a quick and simple example:
Public Function NewSheet(SheetName As String) As Worksheet

Set NewSheet = Worksheets.Add()
NewSheet.Name = SheetName

End Function

In Immediate Pane:
Set MySheet = NewSheet("TEST")
? MySheet.Name
TEST
--
- K Dales


"Jean-Pierre Bidon" wrote:

Hi,
My problem is rather basic but I am stumped.
Here it is: is it possible to return a worksheet object from a user defined
function (possibly via a ByRef argument)?
I'd appreciate an exemple of syntax in order to try it.
Many thanks in advance.
--
Jean-Pierre Bidon
Interstat
91 rue de Rennes
75006 Paris
Tél: 01 45 49 19 17



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Retrun a worksheet from a function

Thank you for your help. However I have still two problems.
First, The sheet that I'd like to return belongs to a workbook that I get
with the function GetObject(). With this function, it appears that the
corresponding workbook stay open, but doesn't have exactly the behavior of
an open workbook. I think that I'll drop it (using GetObject) in favour of
the Open() method.
Secondly, I just realized it in running the exemple; I made a small
subroutines to delete the new sheet just added. Each time I execute the
Delete method in this routine, I got a message to confirm. My question: is
it possible to bypass this message and have the order excuted silently?
Thanks again.
Jean-Pierre

"Jean-Pierre Bidon" a écrit dans le message de news:
...
Hi,
My problem is rather basic but I am stumped.
Here it is: is it possible to return a worksheet object from a user
defined function (possibly via a ByRef argument)?
I'd appreciate an exemple of syntax in order to try it.
Many thanks in advance.
--
Jean-Pierre Bidon
Interstat
91 rue de Rennes
75006 Paris
Tél: 01 45 49 19 17



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Retrun a worksheet from a function

As for the message, that is easy to deal with.
Application.DisplayAlerts=False will turn the message off, then (when done
deleting) set it back on with Application.DisplayAlerts = True.

As for the first part of your question: Yes, my code assumes you are using
ThisWorkbook, but the principle will work as long as the full worksheet
reference is specified - i.e. Workbooks("WorkbookName").Worksheets....

How best to implement this depends on what you need the function to
accomplish. For my simple example of creating a sheet, I could do this:

Public Function NewSheet(Wbook as Workbook, SheetName As String) As Worksheet

Set NewSheet = Wbook.Worksheets.Add()
NewSheet.Name = SheetName

End Function

Or, your suggestion about using a ByRef argument would also work - but then
you don't necessarily need to return a value and can make it a sub instead:

Public Sub NewSheet(ByRef Wbook As Workbook, SheetName As String)

Set NSheet = Wbook.Worksheets.Add()
NSheet.Name = SheetName

End Sub

--
- K Dales


"Jean-Pierre Bidon" wrote:

Thank you for your help. However I have still two problems.
First, The sheet that I'd like to return belongs to a workbook that I get
with the function GetObject(). With this function, it appears that the
corresponding workbook stay open, but doesn't have exactly the behavior of
an open workbook. I think that I'll drop it (using GetObject) in favour of
the Open() method.
Secondly, I just realized it in running the exemple; I made a small
subroutines to delete the new sheet just added. Each time I execute the
Delete method in this routine, I got a message to confirm. My question: is
it possible to bypass this message and have the order excuted silently?
Thanks again.
Jean-Pierre

"Jean-Pierre Bidon" a écrit dans le message de news:
...
Hi,
My problem is rather basic but I am stumped.
Here it is: is it possible to return a worksheet object from a user
defined function (possibly via a ByRef argument)?
I'd appreciate an exemple of syntax in order to try it.
Many thanks in advance.
--
Jean-Pierre Bidon
Interstat
91 rue de Rennes
75006 Paris
Tél: 01 45 49 19 17




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
Vlookup will not retrun value Dave Shultz Excel Discussion (Misc queries) 2 March 5th 10 09:10 PM
Retrun "" (blank) Jay Excel Worksheet Functions 1 October 10th 06 03:47 AM
Reference the worksheet from a multiple worksheet range function ( DBickel Excel Worksheet Functions 1 May 28th 05 03:49 AM
Can the offset worksheet function reference another worksheet AlistairJ Excel Worksheet Functions 2 May 9th 05 06:18 PM
formula/function to copy from worksheet to worksheet Jen Excel Programming 5 January 11th 05 08:22 PM


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