Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Objects in VBA and Excel

Does anyone know how to return a reference to an object
(from a VBA function) to a cell on an Excel worksheet, and
then use that reference (that is sitting in the worksheet
cell) as an argument to another VBA function?

Something like:

Public Function fun1() as myClass
Dim myObject as new myClass
...bunch of code that does stuff
fun1 = myObject
End Function

Public Function fun2(myObj as myClass) as double
fun2 = myObj.getDblValue
End Function

Let's say I want to call fun1 from cell A1 on Sheet1 in my
Excel workbook. Then I want to call fun2 with the
reference to the object created by fun1 (let's say from
cell A2):

in cell A1:
=fun1()

in celll A2:
=fun2(A1)

Why? Well, maybe my fun1 does a tremendous amount of
work. So, I would like to execute it only once, but then
be able to query the object (created by fun1) several
times with functions like fun2, without having to rerun
fun1 every time.

I am pretty sure it is possible to do this, because I have
seen the functionality. But I don't know the technical
details of how to actually do it.

Any help would be greatly appreciated!!



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default Objects in VBA and Excel

Stephen..

Try CALLBYNAME function (see vba help for details),
Note that you need VBA6 (xl2000+) for this

Have fun1 store the string to be used in fun2
Hide the display of the string (it's TEXT property) by giving the cell
that uses fun1 a custom number format like ",,,"

Sub FillMeUp()
[a1].Formula = "=fun1()"
[a2].Formula = "=fun2(a1)"
[a1].NumberFormat = String(3, _
Application.International(xlListSeparator))
End Sub

Function fun1() As String
fun1 = "myclass!myproc!2"
End Function

Function fun2(r As Range) as Double
Dim s As Variant
s = Split(r.Value, "!")

fun2 = CallByName(s(0), s(1), VbGet, s(2))
End Function


keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


"Stephen Bianchi" wrote:

Does anyone know how to return a reference to an object
(from a VBA function) to a cell on an Excel worksheet, and
then use that reference (that is sitting in the worksheet
cell) as an argument to another VBA function?

Something like:

Public Function fun1() as myClass
Dim myObject as new myClass
...bunch of code that does stuff
fun1 = myObject
End Function

Public Function fun2(myObj as myClass) as double
fun2 = myObj.getDblValue
End Function

Let's say I want to call fun1 from cell A1 on Sheet1 in my
Excel workbook. Then I want to call fun2 with the
reference to the object created by fun1 (let's say from
cell A2):

in cell A1:
=fun1()

in celll A2:
=fun2(A1)

Why? Well, maybe my fun1 does a tremendous amount of
work. So, I would like to execute it only once, but then
be able to query the object (created by fun1) several
times with functions like fun2, without having to rerun
fun1 every time.

I am pretty sure it is possible to do this, because I have
seen the functionality. But I don't know the technical
details of how to actually do it.

Any help would be greatly appreciated!!





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Objects in VBA and Excel

What you want to do is doable...kinda...

You would have to retain the information in some kind of a global to
pull this off. Also, a function cannot return an object to XL. So,
something like the foll. would work.

In a class module, Class1:

Option Explicit

Public i As Long
Public Sub doCalculations(p1, p2, p3)
i = p1 * p2 * p3
End Sub

In a standard module:
Option Explicit
Dim x As Class1

Public Function Func1(p1, p2, p3)
Set x = New Class1
x.doCalculations p1, p2, p3
Func1 = x.i
End Function
Public Function Func2()
Func2 = x.i
End Function

Using Func1 in a worksheet with three parameters does all the heavy
duty calculation and stores the result in a global variable. Func2
simply returns an acceptable data type to XL.

For example, in C5, enter =func1(2,3,4) and in C6 =func2()

--
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...
Does anyone know how to return a reference to an object
(from a VBA function) to a cell on an Excel worksheet, and
then use that reference (that is sitting in the worksheet
cell) as an argument to another VBA function?

Something like:

Public Function fun1() as myClass
Dim myObject as new myClass
...bunch of code that does stuff
fun1 = myObject
End Function

Public Function fun2(myObj as myClass) as double
fun2 = myObj.getDblValue
End Function

Let's say I want to call fun1 from cell A1 on Sheet1 in my
Excel workbook. Then I want to call fun2 with the
reference to the object created by fun1 (let's say from
cell A2):

in cell A1:
=fun1()

in celll A2:
=fun2(A1)

Why? Well, maybe my fun1 does a tremendous amount of
work. So, I would like to execute it only once, but then
be able to query the object (created by fun1) several
times with functions like fun2, without having to rerun
fun1 every time.

I am pretty sure it is possible to do this, because I have
seen the functionality. But I don't know the technical
details of how to actually do it.

Any help would be greatly appreciated!!




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
Enlarge objects in excel Herman[_2_] Excel Discussion (Misc queries) 2 July 5th 09 12:32 PM
Pasting objects in Excel Blacktom Excel Discussion (Misc queries) 1 June 23rd 09 02:37 PM
Excel and Busines Objects . . Rachel Costanza[_2_] Excel Discussion (Misc queries) 2 November 11th 08 07:06 AM
PowerPoint objects in Excel suzdapocho Excel Discussion (Misc queries) 0 March 25th 08 06:34 PM
ReOrder Excel Objects. DocBrown Excel Discussion (Misc queries) 2 January 26th 07 02:09 AM


All times are GMT +1. The time now is 11:16 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"