Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
avi avi is offline
external usenet poster
 
Posts: 195
Default Fire Worksheet_change event from module

Hello,

Is there a way to trigger a worksheet_change event from code placed in
a regular module and not in the sheet itself?

Or what is the approach to trigger a worksheet change event when I
don't know in advance which sheet will be involved and don't want to
insert code at each created sheet?

Thanks a lot
Avi



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Fire Worksheet_change event from module

Change Private to Public, eg

Public Sub Worksheet_Change(ByVal Target As Range)

and to call it from elsewhere in your project, eg

Sheet1.Worksheet_Change Range("A1")
Worksheets("Sheet1").Worksheet_Change Range("A1")

Unless you are certain Sheet1 will never be deleted, call with
Worksheets("Sheet1").

Whilst this should work, I think a better way would be to move the bulk of
the event code to a normal module, eg

Sub wsChange(ws As Worksheet, Target As Range)
' code
End Sub

from the worksheet event call it like this
Private Sub Worksheet_Change(ByVal Target As Range)
wsChange Me, Target
End Sub

Call it similarly from anywhere else but change 'Me' to [say] ActiveSheet

Regards,
Peter T



"avi" wrote in message
...
Hello,

Is there a way to trigger a worksheet_change event from code placed in
a regular module and not in the sheet itself?

Or what is the approach to trigger a worksheet change event when I
don't know in advance which sheet will be involved and don't want to
insert code at each created sheet?

Thanks a lot
Avi





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Fire Worksheet_change event from module

Think I misread your question. As OssieMac suggests use the events exposed
in the ThisWorkbook module, if necessary include some If or Select case to
cater for different 'set's of sheets.

Regards,
Peter T


"Peter T" <peter_t@discussions wrote in message
...
Change Private to Public, eg

Public Sub Worksheet_Change(ByVal Target As Range)

and to call it from elsewhere in your project, eg

Sheet1.Worksheet_Change Range("A1")
Worksheets("Sheet1").Worksheet_Change Range("A1")

Unless you are certain Sheet1 will never be deleted, call with
Worksheets("Sheet1").

Whilst this should work, I think a better way would be to move the bulk of
the event code to a normal module, eg

Sub wsChange(ws As Worksheet, Target As Range)
' code
End Sub

from the worksheet event call it like this
Private Sub Worksheet_Change(ByVal Target As Range)
wsChange Me, Target
End Sub

Call it similarly from anywhere else but change 'Me' to [say] ActiveSheet

Regards,
Peter T



"avi" wrote in message
...
Hello,

Is there a way to trigger a worksheet_change event from code placed in
a regular module and not in the sheet itself?

Or what is the approach to trigger a worksheet change event when I
don't know in advance which sheet will be involved and don't want to
insert code at each created sheet?

Thanks a lot
Avi







  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Fire Worksheet_change event from module

Hi Avi,

The code goes in ThisWorkbook module.

The event is the following.

Private Sub Workbook_SheetSelectionChange _
(ByVal Sh As Object, ByVal Target As Range)

Sh is the worksheet and Target is the range.

If you only want the code to run with some worksheets then you can use
If/Then/Else or Select Case and test for the worksheet name/s.

--
Regards,

OssieMac


"avi" wrote:

Hello,

Is there a way to trigger a worksheet_change event from code placed in
a regular module and not in the sheet itself?

Or what is the approach to trigger a worksheet change event when I
don't know in advance which sheet will be involved and don't want to
insert code at each created sheet?

Thanks a lot
Avi



.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Fire Worksheet_change event from module


You could create a class module to handle the change event. You can set
the events in a sheet change event

See chip's webpage 'Events In Excel VBA'
(http://www.cpearson.com/EXCEL/Events.aspx)


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170824

Microsoft Office Help



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 does it fire for a cell changed via foruma? [email protected][_2_] Excel Programming 3 April 10th 08 05:16 PM
event fire Curt Excel Programming 19 March 7th 07 12:29 AM
Can't get Sheet_Change event to fire - please help [email protected] Excel Programming 4 November 30th 06 04:28 AM
Event doesn't fire Frank Xia Excel Discussion (Misc queries) 6 February 11th 06 12:54 AM
Worksheet_Change event won't fire to execute Macro??? jpdill5 Excel Programming 2 February 13th 04 02:34 PM


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