|
-
Nov 10th, 2005, 07:56 PM
#1
Thread Starter
Lively Member
[RESOLVED] MS Excel Events
Hi all,
Is there any way that i can do so as not to trigger the Worksheet_Change event of the Excel Worksheet? I would like to write to 2 cells of the worksheet but my program is looping because it keeps on getting into the Change event.
Here's my code.
Private Sub Worksheet_Change(ByVal Target As Range)
If ActiveCell.Column = 3 And (ActiveCell.Row Mod 2) <> 0 Then
ActiveCell.Offset(-1, 1).Value = ActiveCell.Offset(-1, 0).Value + Sheets_
(1).Cells(ActiveCell.Row - 1, ActiveCell.Column)
ActiveCell.Offset(-2, 1).Value = ActiveCell.Offset(-2, 0).Value + Sheets_
(1).Cells(ActiveCell.Row - 2, ActiveCell.Column)
End If
End Sub
thanks a lot!
-
Nov 10th, 2005, 08:37 PM
#2
Re: MS Excel Events
Maybe by using a Flag like:
VB Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Static blnDone As Boolean
If Not blnDone And (ActiveCell.Column = 3 And (ActiveCell.Row Mod 2) <> 0) Then
ActiveCell.Offset(-1, 1).Value = ActiveCell.Offset(-1, 0).Value + Sheets _
(1).Cells(ActiveCell.Row - 1, ActiveCell.Column)
ActiveCell.Offset(-2, 1).Value = ActiveCell.Offset(-2, 0).Value + Sheets _
(1).Cells(ActiveCell.Row - 2, ActiveCell.Column)
blnDone = True
End If
End Sub
I haven't tested it, but you may get the idea
Last edited by Bruce Fox; Nov 10th, 2005 at 08:45 PM.
Reason: Made the Flag STATIC!
-
Nov 10th, 2005, 09:34 PM
#3
Re: MS Excel Events
A little issue Bruce (which I'm sure you'll be kicking yourself for!), the flag needs to be changed one way before the work starts, and changed back once finished, eg:
VB Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Static blnWorking As Boolean
If Not(blnWorking) Then
blnWorking = True
If (ActiveCell.Column = 3 And (ActiveCell.Row Mod 2) <> 0) Then
ActiveCell.Offset(-1, 1).Value = ActiveCell.Offset(-1, 0).Value + Sheets _
(1).Cells(ActiveCell.Row - 1, ActiveCell.Column)
ActiveCell.Offset(-2, 1).Value = ActiveCell.Offset(-2, 0).Value + Sheets _
(1).Cells(ActiveCell.Row - 2, ActiveCell.Column)
End If
blnWorking = False
End If
End Sub
-
Nov 10th, 2005, 10:35 PM
#4
New Member
Re: MS Excel Events
FWIW, I always use the EnableEvents property of the Application object:
VB Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'Your Code Here
Application.EnableEvents = True
End Sub
HTH
-
Nov 10th, 2005, 10:49 PM
#5
Re: MS Excel Events
Hi si_the_geek,
I left it as is because I figured that once that particular piece of code excutes, that it is no longer requried for that session. Opps maybe I shouln't have assumed that...
Have a great weekend 
Bruce.
-
Nov 11th, 2005, 05:09 AM
#6
Thread Starter
Lively Member
-
Nov 11th, 2005, 07:56 PM
#7
Re: MS Excel Events
 Originally Posted by medison
FWIW, I always use the EnableEvents property of the Application object:
VB Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
'Your Code Here
Application.EnableEvents = True
End Sub
HTH
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|