Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Add Sheet Change Event at Run Time

My project adds a worksheet to the active workbook and then adds a shee
change event procedure at run time. After the procedure is ran, th
vbe window automatically opens to the new sheet change event procedure
Is there a way to prevent this from happening? I have tried turnin
off screen updating and adding the line VBE.MainWindow.Close to the en
of the code, but I get the error "Programmatic Access to Visual Basi
Project is not Trusted." I have signed my project and selected "Trus
access to Visual Basic Project" in the macro security settings and
still get the error. I am using Excel 2002

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,718
Default Excel VBA - Add Sheet Change Event at Run Time

I don't get an error when I do this:

Application.VBE.MainWindow.Visible = False

I do get an ugly flicker though even with screen updating off.

--
Jim Rech
Excel MVP
"cidol " wrote in message
...
My project adds a worksheet to the active workbook and then adds a sheet
change event procedure at run time. After the procedure is ran, the
vbe window automatically opens to the new sheet change event procedure.
Is there a way to prevent this from happening? I have tried turning
off screen updating and adding the line VBE.MainWindow.Close to the end
of the code, but I get the error "Programmatic Access to Visual Basic
Project is not Trusted." I have signed my project and selected "Trust
access to Visual Basic Project" in the macro security settings and I
still get the error. I am using Excel 2002.


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Excel VBA - Add Sheet Change Event at Run Time

Hi,
If each _Change event for each newly added sheet runs similar code, you could use a Class module instead. That is, you don't need to access the vbide programatically, and you don't need to duplicate similar code several times.

1. Create a class named clsSheetEvent:
'----------------------------------------------------------------
Public WithEvents Worksheet As Worksheet

Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Parent.Name & " -- " & Target.Address
'... your event code here
'
End Sub

Private Sub Class_Terminate()
Set Worksheet = Nothing
End Sub
'----------------------------------------------------------------

2. Add a clsSheetEvent global variable. In a module:
Public wshEvent As clsSheetEvent

3. In the sub that adds a sheet:
Public Sub AddSheet()
Dim wsh As Worksheet

Set wsh = ActiveWorkbook.Worksheets.Add
Set wshEvent = New clsSheetEvent 'Create the clsSheetEvent
Set wshEvent.Worksheet = wsh 'Sets its Worksheet member to newly added one
End Sub

Now, after running AddSheet, change a cell in the newly created sheet, it will executed the _Change from the class.
Here, i declared only one clsSheetEvent variable (wshEvent), but you could, for example, store them in a collection instead to have as many as you want.
The interesting thing is that you don't need to add the code in each sheet.

I hope this helps,

Regards,
Sebastien

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel VBA - Add Sheet Change Event at Run Time

Another approach would be to instantiate and use Application level events:

See Chip Pearson's site on this topic (or use Excel VBA Help)
http://www.cpearson.com/excel/appevent.htm

--
Regards,
Tom Ogilvy

"sebastienm" wrote in message
...
Hi,
If each _Change event for each newly added sheet runs similar code, you

could use a Class module instead. That is, you don't need to access the
vbide programatically, and you don't need to duplicate similar code several
times.

1. Create a class named clsSheetEvent:
'----------------------------------------------------------------
Public WithEvents Worksheet As Worksheet

Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Parent.Name & " -- " & Target.Address
'... your event code here
'
End Sub

Private Sub Class_Terminate()
Set Worksheet = Nothing
End Sub
'----------------------------------------------------------------

2. Add a clsSheetEvent global variable. In a module:
Public wshEvent As clsSheetEvent

3. In the sub that adds a sheet:
Public Sub AddSheet()
Dim wsh As Worksheet

Set wsh = ActiveWorkbook.Worksheets.Add
Set wshEvent = New clsSheetEvent 'Create the clsSheetEvent
Set wshEvent.Worksheet = wsh 'Sets its Worksheet member to

newly added one
End Sub

Now, after running AddSheet, change a cell in the newly created sheet, it

will executed the _Change from the class.
Here, i declared only one clsSheetEvent variable (wshEvent), but you

could, for example, store them in a collection instead to have as many as
you want.
The interesting thing is that you don't need to add the code in each

sheet.

I hope this helps,

Regards,
Sebastien



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Add Sheet Change Event at Run Time

Hi, thanks for all the quick responses.

My project basically writes SQL code and then attaches a query objec
to a new worksheet. I have added an option that allows the user to ad
"drill down" functionality to the results.

To do this, I need to know when the query results have been returned s
that the code can add hyperlinks to the approprate fields (which shoul
only happen once) and handle the hyperlink click event (which update
the SQL code from the query object based on the value of the cell tha
is clicked and then adds another query object to another new sheet).

So, I didn't want to use a class module or an application level even
because the event procedures are pretty specific to certain kinds o
sheets.

I thought of using QueryTable AfterRefresh event, but the user can hav
multiple queries running at the same time and I couldn't find an eas
way for the event to know if the user had selected the drill dow
object for a specific query.

I know how to use ADO to return query results in the code and then cop
the record set to a worksheet which would remove the need to know whe
the query results are returned, but some of the queries can take up t
an hour to run and it's not feasible for my users to not have access t
excel for the period the query(ies) are running. Also, using a quer
object makes it easy for the user to cancel the query by deleting th
cell with the query object before the results are returned. An
suggestions?

Again thanks for all your help. I've been trying to teach mysefl V
for the last few months and these forums have been an invaluabl
resource

--
Message posted from http://www.ExcelForum.com



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel VBA - Add Sheet Change Event at Run Time

Hi, thanks for all the quick responses.

My project basically writes SQL code and then attaches a query objec
to a new worksheet. I have added an option that allows the user to ad
"drill down" functionality to the results.

To do this, I need to know when the query results have been returned s
that the code can add hyperlinks to the approprate fields (which shoul
only happen once) and handle the hyperlink click event (which update
the SQL code from the query object based on the value of the cell tha
is clicked and then adds another query object to another new sheet).

So, I didn't want to use a class module or an application level even
because the event procedures are pretty specific to certain kinds o
sheets.

I thought of using QueryTable AfterRefresh event, but the user can hav
multiple queries running at the same time and I couldn't find an eas
way for the event to know if the user had selected the drill dow
object for a specific query.

I know how to use ADO to return query results in the code and then cop
the record set to a worksheet which would remove the need to know whe
the query results are returned, but some of the queries can take up t
an hour to run and it's not feasible for my users to not have access t
excel for the period the query(ies) are running. Also, using a quer
object makes it easy for the user to cancel the query by deleting th
cell with the query object before the results are returned. An
suggestions?

Again thanks for all your help. I've been trying to teach mysefl V
for the last few months and these forums have been an invaluabl
resource

--
Message posted from http://www.ExcelForum.com

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Excel VBA - Add Sheet Change Event at Run Time

cidol wrote ...

I know how to use ADO to return query results
in the code and then copy the record set to a
worksheet which would remove the need to
know when the query results are returned, but
some of the queries can take up to an hour to
run and it's not feasible for my users to not have
access to excel for the period the query(ies) are running.


Have you considered:

- using ADO asynchronously? Requires a class module to sink the
connection/recordset events and get a 'callback' when the query has
finished executing;

- executing the queries on the DB server, creating a temp table and
the Excel clients only query the temp table?

- reviewing your queries for efficiency (an hour to run?! <g) e.g.
are you using a cursor/loop rather than pure SQL?

Jamie.

--
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
Worksheet Change Event - copy cell to another sheet dhstein Excel Discussion (Misc queries) 2 October 12th 09 06:35 PM
Excel Bi-weekly time sheet change dates to Thursday- Wednesday? cainart New Users to Excel 13 July 25th 07 12:50 PM
Cancel sheet change event NSK Charts and Charting in Excel 1 July 17th 07 08:25 PM
Sheet Name Change Event? Bob L. Excel Programming 0 August 18th 03 10:08 PM
Sheet Name Change Event? Bob Phillips[_5_] Excel Programming 0 August 18th 03 09:57 PM


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