PC Review


Reply
Thread Tools Rate Thread

On Change Event and Intersect

 
 
headly
Guest
Posts: n/a
 
      22nd Apr 2008
Have code that reads (thanks to ozgrid)

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

If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
ActiveCell.Offset(0, 2).Value = ActiveCell.Value * ActiveCell.Offset(0,
1).Value
End If

but that effects the range c13:c22 on all sheets; How do I keep it specific
to a particular sheet?

TIA
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      22nd Apr 2008
Use worksheet code rather than workbook code:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
ActiveCell.Offset(0, 2).Value = ActiveCell.Value * ActiveCell.Offset(0,
1).Value
End If
End Sub
--
Gary''s Student - gsnu200781


"headly" wrote:

> Have code that reads (thanks to ozgrid)
>
> Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
>
> If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
> ActiveCell.Offset(0, 2).Value = ActiveCell.Value * ActiveCell.Offset(0,
> 1).Value
> End If
>
> but that effects the range c13:c22 on all sheets; How do I keep it specific
> to a particular sheet?
>
> TIA

 
Reply With Quote
 
paul.robinson@it-tallaght.ie
Guest
Posts: n/a
 
      22nd Apr 2008
Hi
Put the code in the Worksheet_Change event for the sheet (double click
the sheet in the VBE, change (General) at the top to worksheet and
choose the event in the right hand dropdown. For most workbook level
events there is a corresponding worksheet level event.
Alternatively, you can change the existing code to work only on
specified sheets

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

If Sh.Name = "MySheet" then
If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
ActiveCell.Offset(0, 2).Value = ActiveCell.Value *
ActiveCell.Offset(0, 1).Value
End If
End if

End Sub

regards
Paul
On Apr 22, 5:30*pm, headly <hea...@discussions.microsoft.com> wrote:
> Have code that reads (thanks to ozgrid)
>
> Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
>
> If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
> ActiveCell.Offset(0, 2).Value = ActiveCell.Value * ActiveCell.Offset(0,
> 1).Value
> End If
>
> but that effects the range c13:c22 on all sheets; How do I keep it specific
> to a particular sheet?
>
> TIA


 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      22nd Apr 2008
Hi,

I'm guessing but I think you probably want target instead of activecell and
to keep it sheet specific:-

Right click the sheet tab, view code and paste it in on the right

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
Target.Offset(0, 2).Value = Target.Value * Target.Offset(0, 1).Value
End If


End Sub

Mike

"headly" wrote:

> Have code that reads (thanks to ozgrid)
>
> Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
>
> If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
> ActiveCell.Offset(0, 2).Value = ActiveCell.Value * ActiveCell.Offset(0,
> 1).Value
> End If
>
> but that effects the range c13:c22 on all sheets; How do I keep it specific
> to a particular sheet?
>
> TIA

 
Reply With Quote
 
Jim Thomlinson
Guest
Posts: n/a
 
      22nd Apr 2008
Your code should be in the sheet rather than in ThisWorkbook. Right click the
sheet tab and select view code. Paste the following...

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo ErrorHandler
If Target.Count > 1 Then Exit Sub
Application.EnableEvents = False
If Not Intersect(Target, Range("C13:C22")) Is Nothing Then _
Target.Offset(0, 2).Value = Target.Value * Target.Offset(0, 1).Value
ErrorHandler:
Application.EnableEvents = True
End Sub

Note that there are a couple of changes. If more than a single cell was
changed then nothing happens. If the change occured in C13:C22 then it turns
off events and makes the change which keeps the code from making a recursive
call based in the change that the code makes.
--
HTH...

Jim Thomlinson


"headly" wrote:

> Have code that reads (thanks to ozgrid)
>
> Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
>
> If Not Intersect(Target, Range("c13:c22")) Is Nothing Then
> ActiveCell.Offset(0, 2).Value = ActiveCell.Value * ActiveCell.Offset(0,
> 1).Value
> End If
>
> but that effects the range c13:c22 on all sheets; How do I keep it specific
> to a particular sheet?
>
> TIA

 
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
apply cell change event to single column - WorksheetChange Event MiataDiablo@gmail.com Microsoft Excel Programming 5 4th May 2008 02:28 AM
Problem with CommandBarComboBox Change Event (Event fires only once) M. Khalid Farooq Microsoft Outlook Program Addins 1 19th Oct 2006 02:34 PM
Control where change event does not trigger click event? =?Utf-8?B?c3dvcmRmaXNo?= Microsoft Powerpoint 2 17th Jul 2006 06:21 PM
MsgBox in Enter event causes combobox not to run Change event =?Utf-8?B?UmljaGFyZA==?= Microsoft Excel Programming 0 6th Mar 2006 02:52 PM
Datagrid cell change event? Where to find event. Roger Microsoft VB .NET 4 29th Mar 2005 09:45 PM


Features
 

Advertising
 

Newsgroups
 


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