PC Review


Reply
Thread Tools Rate Thread

Attaching code to ThisWorkbook

 
 
Patrick C. Simonds
Guest
Posts: n/a
 
      15th Apr 2009
I placed this code under ThisWorkbook but it does not run. If I place the
code on a worksheet it runs fine for that worksheet. My workbook has over 90
worksheets. I was hoping not to have to attach this code to each worksheet.
Am I missing something?



Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)

If Not Application.Intersect(Target, range("H11:H22")) Is Nothing Then
VacationTaken.Show


End Sub

 
Reply With Quote
 
 
 
 
Jacob Skaria
Guest
Posts: n/a
 
      15th Apr 2009
Double click This workbook. and drop down to get the below event

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

If Not Application.Intersect(Target, range("H11:H22")) Is Nothing Then
VacationTaken.Show

End Sub
--
If this post helps click Yes
---------------
Jacob Skaria


"Patrick C. Simonds" wrote:

> I placed this code under ThisWorkbook but it does not run. If I place the
> code on a worksheet it runs fine for that worksheet. My workbook has over 90
> worksheets. I was hoping not to have to attach this code to each worksheet.
> Am I missing something?
>
>
>
> Option Explicit
> Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)
>
> If Not Application.Intersect(Target, range("H11:H22")) Is Nothing Then
> VacationTaken.Show
>
>
> End Sub
>
>

 
Reply With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      15th Apr 2009
selection change is a workSHEET event, and should be in the worksheet for
which you want to trap the event. Sheet Selection Change is the workbook
event, and this should be in the code page for ThisWorkbook.
HINT: in the code module for the worksheet, your objects are Worksheet and
general, for ThisWorkbook, the objects are Workbook and general

"Patrick C. Simonds" wrote:

> I placed this code under ThisWorkbook but it does not run. If I place the
> code on a worksheet it runs fine for that worksheet. My workbook has over 90
> worksheets. I was hoping not to have to attach this code to each worksheet.
> Am I missing something?
>
>
>
> Option Explicit
> Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)
>
> If Not Application.Intersect(Target, range("H11:H22")) Is Nothing Then
> VacationTaken.Show
>
>
> End Sub
>
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      15th Apr 2009
Just to add...

I wouldn't type those events. Use the dropdown and choose the event that you
want. All you can do by typing is screw it up <vbg>.

And on top of that, I'd qualify the range, too:

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

If Application.Intersect(Target, Sh.Range("H11:H22")) Is Nothing Then
'do nothing
Else
vacationTaken.Show
End If

End Sub

I didn't use "not" just because I find this syntax easier to understand. But
it's a personal choice.



"Patrick C. Simonds" wrote:
>
> I placed this code under ThisWorkbook but it does not run. If I place the
> code on a worksheet it runs fine for that worksheet. My workbook has over 90
> worksheets. I was hoping not to have to attach this code to each worksheet.
> Am I missing something?
>
> Option Explicit
> Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)
>
> If Not Application.Intersect(Target, range("H11:H22")) Is Nothing Then
> VacationTaken.Show
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
meh2030@gmail.com
Guest
Posts: n/a
 
      15th Apr 2009
On Apr 15, 12:26*pm, "Patrick C. Simonds" <ordnan...@comcast.net>
wrote:
> I placed this code under *ThisWorkbook *but it does not run. If I place the
> code on a worksheet it runs fine for that worksheet. My workbook has over90
> worksheets. I was hoping not to have to attach this code to each worksheet.
> Am I missing something?
>
> Option Explicit
> Private Sub Worksheet_SelectionChange(ByVal Target As Excel.range)
>
> If Not Application.Intersect(Target, range("H11:H22")) Is Nothing Then
> VacationTaken.Show
>
> End Sub


Patrick,

Your Worksheet event does not run in the Workbook class because the
Workbook class handles Workbook events. In other words,
Worksheet_SelectionChange is specific to a worksheet class.
Workbook_SheetSelectionChange is specific to a workbook class. Try
placing your code into the Workbook_SheetSelectionChange event within
ThisWorkbook and see if you receive the results you are looking for.

Best,

Matthew Herbert
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ThisWorkbook populate with Code Q Seanie Microsoft Excel Programming 7 27th Jan 2009 10:17 AM
Turn off ThisWorkbook Code? volodind@gmail.com Microsoft Excel Programming 3 12th Apr 2007 02:29 PM
Populate ThisWorkbook via Code Sean Microsoft Excel Programming 13 14th Jan 2007 06:03 PM
Excel is Ignoring ThisWorkbook.Save event after Thisworkbook.Close call John Fuller Microsoft Excel Programming 5 30th Aug 2006 09:57 PM
Importing Code into 'ThisWorkbook' Mark Microsoft Excel Programming 3 16th Apr 2004 03:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:53 PM.