+ Reply to Thread
Results 1 to 3 of 3

worksheet change code not working

Hybrid View

nadie111 worksheet change code not... 06-24-2008, 12:37 PM
Leith Ross Hello nadie111, The... 06-24-2008, 02:39 PM
nadie111 Thank you very much! 06-24-2008, 04:36 PM
  1. #1
    Registered User
    Join Date
    06-20-2008
    Posts
    3

    worksheet change code not working

    Hello Nadie, welcome to the forum.

    Please take the trouble to read the forum rules, and note the one which asks posters to wrap VBA code in code tags.

    I've edited the code on this occasion for you.

    Regards


    Hello all,

    I'm relatively new to macros, so my apologies if this is beyond basic. I had this working at one point, to my delight! but then I changed something and made an infinite loop and had to quit and now nothing is happening, sadly.

    I want a message box to pop up when there is numerical input into cell Q9 of either less than 500 or less than 1000. If the cell is less than 500, I want the cell to revert to blank.

    I'm still working on how to make the cell font colour red if the value is between 500 and 1000 using a macro rather than conditional formatting.

    Thanks so much for your help!

    Nadie
    Here is my code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("Q9")) Is Nothing Then
        If IsNumeric(Target) Then
        
            Application.EnableEvents = True
            Select Case Target
                Case Is < 500
                
                MsgBox ("  Impossible. Over 1000 is recommended.  ")
                Target = " "
                Case Is < 1000
                MsgBox ("  Over 1000 is recommended.  ")
    
            End Select
            
        
        End If
    End If
    End Sub
    Last edited by Richard Buttrey; 06-24-2008 at 12:45 PM. Reason: Code tags missing from code section of post.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello nadie111,

    The purpose of the statement Application.EnableEvents is to enable or disable Excel from responding to any other generated events while the macro is running. This is important with events that are triggered by a change event, like changing a cell's value, or selecting a different cell.

    If these other events are not disabled, you can trigger them by changing a cell's value or activating another cell in your code. The event will keep calling itself over and over until the stack overflows and generates an error. The Target's (cell's) value is changed in your code in the Case Statement. This will cause the error to occur because the event has not been disabled.
    Private Sub Worksheet_Change(ByVal Target As Range)
      If Not Application.Intersect(Target, Range("Q9")) Is Nothing Then
        If IsNumeric(Target) Then
            Application.EnableEvents = False
            Select Case Target
                Case Is < 500          
                  MsgBox ("  Impossible. Over 1000 is recommended.  ")
                  Target = " "
                Case Is < 1000
                  MsgBox ("  Over 1000 is recommended.  ")
            End Select 
            Apllication.EnableEvents = True        
        End If
      End If
    End Sub
    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    06-20-2008
    Posts
    3

    Thank you very much!

    It worked! I think that events must have been disabled in the entire worksheet, which may have been causing me problems, because after I made the change you suggested, once again, not a single one of my macros were working. That was why I put the phrase in there in the first place.

    So I inserted the "Application.EnableEvents = True" into my Private Sub Workbook_Open() and now everything runs perfectly. (I hope that insertion won't kick me later)

    Once again, thank you for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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