Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Execute class method on all worksheets?

Hi,

I have an class instance on all my worksheets, is there some way I
can dynamically execute a method on these objects? Something like

for each worksheet in worksheets
worksheet.myObject.myMethod
next worksheet

?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Execute class method on all worksheets?

In principle you can call a method within a class passing an object to it,
so yes. But the class method would have to accept a worksheet as an
argument. The syntax would be

In the class

Function myMethod (sh As Worksheet)
Msgbox sh.Name
End Function

and in the general module

Set myObject = New class_name

For Each ws In Activeworkbook.Worksheets
MyObject.MyMethod ws
Next ws


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Nik" wrote in message
oups.com...
Hi,

I have an class instance on all my worksheets, is there some way I
can dynamically execute a method on these objects? Something like

for each worksheet in worksheets
worksheet.myObject.myMethod
next worksheet

?



  #3   Report Post  
Posted to microsoft.public.excel.programming
Art Art is offline
external usenet poster
 
Posts: 587
Default Execute class method on all worksheets?

Nik,

I'm not sure if this is what you want. You probably need to set up a
collection to hold the instances of your class -- this needs to be set up in
a module -- not a class module. See the following:

***I set up the following Class (named Class1)

--------------------------------------------------------
Dim mTheSheet As Worksheet

Sub test()
TheSheet.Cells(1, 1) = TheSheet.Name
End Sub

Property Let TheSheet(ByVal Value As Worksheet)
Set mTheSheet = Value
End Property
Property Get TheSheet() As Worksheet
Set TheSheet = mTheSheet
End Property
--------------------------------------------------------

***I have 3 worksheets, and in each of them I have the following:

--------------------------------------------------------
Private Sub Worksheet_Activate()
Dim x As New Class1
x.TheSheet = Me
cc.Add x
End Sub
--------------------------------------------------------

***Then in a regular module I have the following:

--------------------------------------------------------
Public cc As New Collection

Sub Main()
For Each WS In Worksheets
WS.Activate
Next WS
RunMethods
End Sub

Sub RunMethods()
Dim x As Class1
For Each x In cc
x.test
Next
End Sub
--------------------------------------------------------
***
Running Main accomplishes the goals of executing the method in each of the
classes instantiated by activated each of the worksheets.

I hope this is of help.

"Nik" wrote:

Hi,

I have an class instance on all my worksheets, is there some way I
can dynamically execute a method on these objects? Something like

for each worksheet in worksheets
worksheet.myObject.myMethod
next worksheet

?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Execute class method on all worksheets?

I'm not sure if this is what you want. You probably need to set up a
collection to hold the instances of your class -- this needs to be set up in
a module -- not a class module. See the following:


Aah, thanks. That was what I was looking for. Too bad events have to
be enabled for it to work so
there is some flickering but at least it works.

  #5   Report Post  
Posted to microsoft.public.excel.programming
Art Art is offline
external usenet poster
 
Posts: 587
Default Execute class method on all worksheets?

Okay -- I never thought about events and screenupdating before. I know it's
tangential to you initial concern, but after reading your post I tried a few
things and realize I don't understand how screenupdating is controlled --
maybe you know?

When you mentioned that you get flickers because you need events I figured
that you couldn't turn off updating and still get your events. (I may have
completely misunderstood, and you were considering turning off events.)
Anyway, I wrote a small progam to set screenupdating=false. Then put an
SelectionChange event in a worksheet to add the value of screenupdating to a
collection (didn't want to mess it up with a msgbox). This was followed by a
stop.

I looked at the collection (in the immediate pane) and saw that
screenupdating had been turned to true. I did check by add it's status to
the collection immediately after setting it (in a macro in a module).

Do you know how this works? Does an event being triggered automatically
turn it back on? Aren't you sorry you put in that gratuitous information?
<g.



"Nik" wrote:

I'm not sure if this is what you want. You probably need to set up a
collection to hold the instances of your class -- this needs to be set up in
a module -- not a class module. See the following:


Aah, thanks. That was what I was looking for. Too bad events have to
be enabled for it to work so
there is some flickering but at least it works.


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
Add method of Workbooks class failed Bill Baker[_2_] Excel Programming 0 November 6th 06 04:33 PM
Copy method of worksheet class failed - loop copying worksheets lif[_12_] Excel Programming 0 July 31st 06 10:02 PM
Should Method Belong to the Class? Trip[_3_] Excel Programming 1 September 20th 05 09:29 PM
Specify which method is default in custom class Bing Excel Programming 1 December 23rd 04 10:33 PM
Calling from C#.Net App Office._CommandBarButton.Execute() method in Excel 2003 throws a COMException Jacek Excel Programming 1 December 21st 03 03:48 PM


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