Results 1 to 7 of 7

Thread: [RESOLVED] MS Excel Events

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    manila, philippines
    Posts
    74

    Resolved [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!

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: MS Excel Events

    Maybe by using a Flag like:
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Static blnDone As Boolean
    3.  
    4.     If Not blnDone And (ActiveCell.Column = 3 And (ActiveCell.Row Mod 2) <> 0) Then
    5.         ActiveCell.Offset(-1, 1).Value = ActiveCell.Offset(-1, 0).Value + Sheets _
    6.         (1).Cells(ActiveCell.Row - 1, ActiveCell.Column)
    7.         ActiveCell.Offset(-2, 1).Value = ActiveCell.Offset(-2, 0).Value + Sheets _
    8.         (1).Cells(ActiveCell.Row - 2, ActiveCell.Column)
    9.         blnDone = True
    10.     End If
    11.    
    12. 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!

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2. Static blnWorking As Boolean
    3.    If Not(blnWorking) Then
    4.      blnWorking = True
    5.  
    6.     If (ActiveCell.Column = 3 And (ActiveCell.Row Mod 2) <> 0) Then
    7.         ActiveCell.Offset(-1, 1).Value = ActiveCell.Offset(-1, 0).Value + Sheets _
    8.         (1).Cells(ActiveCell.Row - 1, ActiveCell.Column)
    9.         ActiveCell.Offset(-2, 1).Value = ActiveCell.Offset(-2, 0).Value + Sheets _
    10.         (1).Cells(ActiveCell.Row - 2, ActiveCell.Column)
    11.     End If
    12.     blnWorking = False
    13.   End If
    14. End Sub

  4. #4
    New Member
    Join Date
    Apr 2004
    Posts
    12

    Re: MS Excel Events

    FWIW, I always use the EnableEvents property of the Application object:


    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.      Application.EnableEvents = False
    3.      'Your Code Here
    4.      Application.EnableEvents = True
    5. End Sub


    HTH

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Location
    manila, philippines
    Posts
    74

    Re: MS Excel Events

    thanks a lot

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: MS Excel Events

    Quote Originally Posted by medison
    FWIW, I always use the EnableEvents property of the Application object:


    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.      Application.EnableEvents = False
    3.      'Your Code Here
    4.      Application.EnableEvents = True
    5. 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
  •  



Click Here to Expand Forum to Full Width