+ Reply to Thread
Results 1 to 5 of 5

Worksheet_Change function keeps crashing...why??

  1. #1
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Worksheet_Change function keeps crashing...why??

    Hi, i have this code below and every time i make a change to the worksheet, Excel crashes. Whats the problem?
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2003
    Location
    British Columbia , Canada
    MS-Off Ver
    03,07,10,13
    Posts
    727

    Re: Worksheet_Change function keeps crashing...why??

    Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False
    Lastrow = Worksheets("Edit").Cells(Worksheets("Edit").Rows.Count, "A").End(xlUp).Row
    SEQ = 1
    For I = 2 To Lastrow
    If Cells(I, 6).Value = "GOOD" Or Cells(I, 6).Value = "BAD" Then
    Cells(I, 7).Value = SEQ
    SEQ = SEQ + 1
    Else: Cells(I, 7).Value = ""
    End If

    Next I

    Application.EnableEvents = True
    End Sub

    EXPLAINATION:
    each of your changes in the loop triggered the Worksheet_change Event ... you need to turn this off before changing fields and then turn on again after e.g. Application.EnableEvents
    Last edited by nimrod; 05-25-2016 at 07:05 PM.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,366

    Re: Worksheet_Change function keeps crashing...why??

    A) because you are making changes to the worksheet within the Worksheet Change event handler, B) because you are not restricting the cells you monitor and C) you're looping through all the used cells in a column and changing them.

    Use Application.EnableEvents = False before changes and True afterwards.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Worksheet_Change function keeps crashing...why??

    Quote Originally Posted by nimrod View Post
    private sub worksheet_change(byval target as range)

    application.enableevents = false
    lastrow = worksheets("edit").cells(worksheets("edit").rows.count, "a").end(xlup).row
    seq = 1
    for i = 2 to lastrow
    if cells(i, 6).value = "good" or cells(i, 6).value = "bad" then
    cells(i, 7).value = seq
    seq = seq + 1
    else: Cells(i, 7).value = ""
    end if

    next i

    application.enableevents = true
    end sub

    explaination:
    Each of your changes in the loop triggered the worksheet_change event ... You need to turn this off before changing fields and then turn on again after
    beauty !! Thank you !

  5. #5
    Forum Contributor
    Join Date
    12-11-2009
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    517

    Re: Worksheet_Change function keeps crashing...why??

    Quote Originally Posted by nimrod View Post
    private sub worksheet_change(byval target as range)

    application.enableevents = false
    lastrow = worksheets("edit").cells(worksheets("edit").rows.count, "a").end(xlup).row
    seq = 1
    for i = 2 to lastrow
    if cells(i, 6).value = "good" or cells(i, 6).value = "bad" then
    cells(i, 7).value = seq
    seq = seq + 1
    else: Cells(i, 7).value = ""
    end if

    next i

    application.enableevents = true
    end sub

    explaination:
    Each of your changes in the loop triggered the worksheet_change event ... You need to turn this off before changing fields and then turn on again after
    beauty !! Thank you !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel 2010 crashing / Function debugging
    By tigfur in forum Excel General
    Replies: 0
    Last Post: 04-27-2016, 07:20 AM
  2. Excel crashing when trying to view code before macro run; not crashing after first run
    By goonerforlyf in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2015, 05:27 PM
  3. cell function is not working in worksheet_change
    By gan_xl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-29-2013, 02:07 AM
  4. Worksheet_Change function
    By sunvin in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2007, 05:37 AM
  5. Adding More than One Worksheet_Change Function
    By B. Baumgartner in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-02-2007, 12:26 PM
  6. Worksheet_Change function
    By pimar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-20-2006, 03:46 AM
  7. Worksheet_Change function
    By corina7sis in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-24-2005, 12:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1