+ Reply to Thread
Results 1 to 17 of 17

Why Message Boxes Repeat?

Hybrid View

Sgt Rock Why Message Boxes Repeat? 09-06-2014, 09:13 AM
Richard Buttrey Re: Why Message Boxes Repeat? 09-06-2014, 09:56 AM
MickG Re: Why Message Boxes Repeat? 09-06-2014, 10:04 AM
coolblue Re: Why Message Boxes Repeat? 09-06-2014, 10:08 AM
Sgt Rock Re: Why Message Boxes Repeat? 09-06-2014, 10:40 AM
Sgt Rock Re: Why Message Boxes Repeat? 09-06-2014, 10:45 AM
coolblue Why Message Boxes Repeat? 09-06-2014, 10:54 AM
Sgt Rock Re: Why Message Boxes Repeat? 09-06-2014, 11:17 AM
coolblue Re: Why Message Boxes Repeat? 09-06-2014, 11:25 AM
Sgt Rock Re: Why Message Boxes Repeat? 09-06-2014, 11:41 AM
coolblue Re: Why Message Boxes Repeat? 09-06-2014, 11:46 AM
Sgt Rock Re: Why Message Boxes Repeat? 09-06-2014, 12:03 PM
Sgt Rock Re: Why Message Boxes Repeat?... 09-07-2014, 07:49 AM
coolblue Re: Why Message Boxes Repeat? 09-07-2014, 03:07 PM
Sgt Rock Re: Why Message Boxes Repeat? 09-07-2014, 03:28 PM
xladept Re: Why Message Boxes Repeat? 09-07-2014, 04:21 PM
Sgt Rock Re: Why Message Boxes Repeat? 09-07-2014, 04:27 PM
  1. #1
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Why Message Boxes Repeat?

    I am Googled and Forum searched out! Most likely there is a simple explanation for this, but I have yet to find it.
    How to put in words....I have a worksheet change event on a particular cell. If that cell changes then a sub is called
    to "do something." If the cell = a specified name, another sub is called to display a first msgbox, and depending on
    the response (Yes or No) either of 2 msgboxes are displayed. Let's say if "Yes" is clicked, msgbox 2 is displayed,
    to which OK is clicked. On the other hand if "No" is clicked on the first msgbox, msgbox 3 is displayed, to which
    OK is also clicked. The problem is if I answer "Yes" on the 1st msgbox, and OK on the 2nd msg box, they repeat.
    That is, I see msgbox 1 again, and I click "Yes" again, but this time when msgbox 2 appears and I click OK, this
    time it disappears like it should. The same thing happens if "No" is selected on the 1st msgbox. I think(?) it has to
    do with the worksheet change event? There are supposed fixes - Application.??? = false/true - forgot the second
    term, but that didn't work. I guess it would help to post the code - Any assistance would be most, most appreciated,
    since I at wits end trying to figure out what I'm doing wrong. Thank you all in Advance!!! It is the sub - CheckForLead
    that is giving me fits! Sgt. Rock!!
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This line of code clears the Activity column once a new chemical is selected from the select chemical drop down
    
        If Not Intersect(Target, Target.Worksheet.Range("C4")) Is Nothing Then Call ClearColumn
                   
        If Range("C4").Value = "Lead" Then Call CheckForLead
               
    End Sub
    
    Sub ClearColumn()
        With Worksheets("Threshold").ListObjects("ThresholdInputTable").ListColumns("ActivityTRIChemical")
            .DataBodyRange.ClearContents
        End With
    End Sub
    
    Sub CheckForLead()
    
    'Check if lead is contained in stainless steel, brass or bronze alloy
    
        Dim myTitle As String
        Dim MyMsg As String
        Dim Response As VbMsgBoxResult
                
                If Worksheets("Threshold").Range("C4").Value = "Lead" Then
                myTitle = "Qualify Lead & Lead Compounds"
                MyMsg = "Is the lead contained in stainless steel, brass, or bronze alloy?"
                Response = MsgBox(MyMsg, vbQuestion + vbYesNo, myTitle)
           
                If Response = vbYes Then
                    MsgBox "The normal thresholds of 25,000 pounds (Mfg,Proc) or 10,000 pounds (OWU) apply"
                        
                ElseIf Response = vbNo Then
                    MsgBox "The PBT 100 pound theshold applies to lead in all activities"
                Exit Sub
                
            End If
            End If
    
    End Sub

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Why Message Boxes Repeat?

    Hi,

    I find your description quite hard to follow and can't help thinking that it would be much easier to offer a solution if you could put a simple process flow diagram together which clearly shows the events and resultant actions.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Why Message Boxes Repeat?

    It could be that you code changes the value of the "Target" cell and therefore runs the code again ""
    Try adding :- "Application. EnableEvent" code as shown below'
    NB:- Untested !!
    Private Sub Worksheet_Change(ByVal Target As Range)
    'This line of code clears the Activity column once a new chemical is selected from the select chemical drop down
    Application.EnableEvents = False
        
        If Not Intersect(Target, Target.Worksheet.Range("C4")) Is Nothing Then Call ClearColumn
                   
        If Range("C4").Value = "Lead" Then Call CheckForLead
    
    Application.EnableEvents = True
        End Sub

  4. #4
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Why Message Boxes Repeat?

    I agree with @Richard Buttery but my gut feeling is that the change event is firing for some reason. Perhaps you need the bracket your code in the event handler with Application.EnableEvents = False ... Application.EnableEvents = True
    You also need to ensure that the second statement is executed in the event of error-ing out of the event handler.


    Sent from my iPad using Tapatalk

  5. #5
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    Apologize for the convoluted description - hence why I included the code.
    Have tried the Application...enable true/false to no avail - agree, "something" is triggering
    the msgboxes to show again, but on second time through, they do as they should - simply close.
    I first thought Exit Sub was not working...but that is not the case.
    Possibly, just possibly due to the fact it's the same cell (C4)for the change event - but also for the msgboxes as well...???
    Thoughts, suggestions?

    Cell C4 is a pull down of various chemicals from a table on a different worksheet - when the name changes - a column is cleared on the same worksheet as C4 - simple.
    However, and here's the kicker, if there happens to be a particular name in that cell, C4, e.g. lead (Pb), then I "invoke" a message box which asks a question.
    If the response is "yes" - then another message pop's up, whereupon I want to simply click OK, and exit, but it doesn't do that -
    goes back to the first message box. If I answer the same way Yes, the OK, it exits. Same is True if I initially answer "No" to the first msgbox - a subsequent message
    appears - simply making a statement, to which I click OK, and again should exit but does the same thing as above - goes again but on 2nd go through, it will exit.

    I most appreciate your viewing this since there are a gazillion questions on here and I do try my very best to solve on my own. Have done good so far in this program,
    but this issue as really thrown me for a big loop!!!

    Best weekend wishes,
    Sgt Rock (Mort in Dallas)

  6. #6
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    FYI...did as you suggest and added the Application code...
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This line of code clears the Activity column once a new chemical is selected from the select chemical drop down
    
    Application.EnableEvents = False
        If Not Intersect(Target, Target.Worksheet.Range("C4")) Is Nothing Then Call ClearColumn
                   
        If Range("C4").Value = "Lead" Then Call CheckForLead
    Application.EnableEvents = True
    End Sub
    Now no message boxes at all appear even if Lead appears in cell C4 - this strange issue has happened at lot - I change the code a lil bit, and it stops working - have to go back to
    the original workbook to get it going again...I'm stumped!!!
    Best, SR

  7. #7
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Why Message Boxes Repeat?

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This line of code clears the Activity column once a new chemical is selected from the select chemical drop down
    
    Application.EnableEvents = False
    
    On Error GoTo exitEnabled
    
        If Not Intersect(Target, Target.Worksheet.Range("C4")) Is Nothing Then Call ClearColumn
                   
        If Range("C4").Value = "Lead" Then Call CheckForLead
    
    exitEnabled:
    Application.EnableEvents = True
    End Sub

    Code edited to make it safe.
    Have you set a break point in the error handler and stepped through?
    And searched for other calls to CheckForLead?


    Sent from my iPad using Tapatalk

  8. #8
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    Thanks "CoolBlue"
    Tried the additions and nada. The more I read the more I know it has something to do with events and I think this issue is the same cells is associated with 2 events,
    which may be inappropriate or not possible?
    Event 1 is if the C4 changes to a new chemical, then ClearColumn is called and a column in a Table is cleared of information, ready for a new entry in Cell C4.
    Event 2 is if the chemical in C4 happens to be lead (Pb) then I want some messages to appear to alert the user of issues with lead that must be known.

    I guess the question is, can a single cell have two events associated with it, and if not, what is a work around??? That I have not Googled as yet and will do so now.
    Thanks again for your suggestions...
    Sgt Rock (Mort in Dallas)

  9. #9
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Why Message Boxes Repeat?

    I didn't really think that would fix it, it's just very dangerous not to do it because EventsEnabled is not reset until excel is re-started.
    I don't think you have two event handlers... Unless you have another Worksheet Change event in ThisWorkbook, which I doubt...
    All you need to do is, in the event handler, detect which change occurred and call the appropriate routine.


    Sent from my iPhone using Tapatalk

  10. #10
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    Thanks CoolBlue....makes perfect sense - decide which change occurred and the call the appropriate sub
    You are correct - this is the ONLY change event in the entire workbook.
    Best for your weekend!! I do most appreciate you taking your time to look over - most, most courteous!!
    I'm still "new" to VBA - and felt I accomplished a lot by just realizing what in blue-blazes is wrong,
    much less how to fix it! Smile.
    Sgt Rock (Mort in Dallas)

  11. #11
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Why Message Boxes Repeat?

    Spend some time figuring out how to use debug.print with the immediate window and setting break points and using the watch window. Also find the MSDN library elements referring to vba and excel. This is what you need to help you to methodically work through your code.


    Sent from my iPhone using Tapatalk

  12. #12
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    Most appreciate CoolBlue!!!
    Have never heard of debug.print - will Google
    Do about the Immediate window though

  13. #13
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat? - SOLVED!!!

    After everyone's assistance I feel it only fitting to post the VBA code that works. An associate was able to steer me
    in the right direction. I almost had it. Again the issue was with a "worksheet change event" and having a "second event"
    on the same cell to test. 1st event, is clear a column in a Table once cell C4 is changed to a new chemical.
    2nd event is if cell C4 = Lead then give some msgboxes stating various issues with Lead. You can "see" the messages
    in the code below. Hopefully this code may be of some use for someone to use in a "look-a-like" situation?

    Best wishes to the Forum and their helpfulness!!
    Sgt Rock (Mort in Dallas)

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This line of code clears the Activity column once a new chemical is selected from the select chemical drop down
    
    If Not Intersect(Target, Target.Worksheet.Range("C4")) Is Nothing Then
         With Worksheets("Threshold").ListObjects("ThresholdInputTable").ListColumns("ActivityTRIChemical")
            .DataBodyRange.ClearContents
         End With
         
    'If Lead is selected as the chemical whose threshold needs to be calculated
    'Warn the user that threshold could be the original 25,000 lbs/yr - 10,000 lbs/year (if contained in certain metals)
    'Or that the lower 100 lbs/year PBT thrshold might apply
        
         If Range("C4").Value = "Lead" Then
            
            Dim myTitle As String
            Dim MyMsg As String
            Dim Response As VbMsgBoxResult
                
                If Worksheets("Threshold").Range("C4").Value = "Lead" Then
                     myTitle = "Qualify Lead & Lead Compounds"
                     MyMsg = "Is the lead contained in stainless steel, brass, or bronze alloy?"
                     Response = MsgBox(MyMsg, vbQuestion + vbYesNo, myTitle)
           
                     If Response = vbYes Then
                        MsgBox "The normal thresholds of 25,000 pounds (Mfg,Proc) or 10,000 pounds (OWU) apply"
                        Exit Sub
                     End If
            
                     If Response = vbNo Then
                        MsgBox "The PBT 100 pound theshold applies to lead in all activities"
                        Exit Sub
                     End If
                End If
        End If
    End If
               
    End Sub

  14. #14
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Why Message Boxes Repeat?

    Good job Sarge 😉


    Sent from my iPad using Tapatalk

  15. #15
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    Hey hope it helps...have developed another question but I suppose it requires a new thread...
    Best CoolBlue...Mort

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Why Message Boxes Repeat?

    Hi Mort,

    Glad you figured it out - but you need to mark this thread as solved - go to thread tools up top
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  17. #17
    Registered User
    Join Date
    08-02-2012
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: Why Message Boxes Repeat?

    Thanks for letting me know how to mark thread as solved Forum!!
    Mort

+ 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. Repeat Message box routine with IF condition and condnl formatting
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-18-2014, 02:39 AM
  2. Message Boxes
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2006, 02:32 PM
  3. [SOLVED] How can I repeat a macro, and how can make it without any message?
    By emil in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-17-2006, 04:35 PM
  4. Message boxes
    By JaB in forum Excel General
    Replies: 1
    Last Post: 11-10-2005, 08:10 AM
  5. Message Boxes.
    By Donny in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-07-2005, 03:06 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