+ Reply to Thread
Results 1 to 5 of 5

One macro prevents another from working

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2009
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    66

    One macro prevents another from working

    Hi all,
    I have a macro built in to one of my worksheets (under view code).

    This prompts a user to select either “Ok” or “Cancel” from a message box.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Set t = Target
    Set b = Range("B:B")
    
    
    If Intersect(t, b) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    x = MsgBox("RESET CALCULATIONS??'", vbOKCancel, "Reset?")
    If x = vbCancel Then Exit Sub
    
    ActiveSheet.Unprotect 
    
    ActiveCell.Offset(-1, 11).Select
    
    
    Selection.FormulaR1C1 = _
            "=IF(AVERAGE(Table!R3C4:R20C4)>5,IF(AVERAGE(Table!R3C4:R20C4)<31,IF(INDIRECT(""b""&ROW())=0,"""",IF(INDIRECT(""q""&ROW())=""X"",IF(INDIRECT(""y""&ROW())=1,INDIRECT(""u""&ROW()),""""),"""")),""""),"""")"
    ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, AllowFiltering:=True, 
    End Sub
    I also have a macro that is designed to run upon save or closure of the workbook which is built in to “this workbook”.

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
         
    
    
    ThisWorkbook.Activate
    ThisWorkbook.Sheets("Main").Select
    Range("A5:O310").Select
        Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    Range("B5").Select
    End Sub
    No for some reason, if “cancel” is selected from the first macro – it prevents the second macro from running on closure. If “ok” (or the first macro is not run) the second one will run perfectly.

    Can anyone help please??

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: One macro prevents another from working

    You have to turn ENABLEEVENTS back on at the bottom of the Worksheet_Change macro:
    Application.EnableEvents = False
    
    your code....
    
    Application.EnableEvents = True
    This piece of code MEANS "turn off other macros for now"...so you have to turn them back on when you're done with your code.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: One macro prevents another from working

    In fact, I would reorder your macro events to this order, it appears you could accidentally abort your code with the MSGBOX and still leave the events turned off:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("B:B")) Is Nothing Then
        
            If MsgBox("RESET CALCULATIONS??'", vbYesNo, "Reset?") = vbNo Then Exit Sub
        
            Application.EnableEvents = False
        
            ActiveSheet.Unprotect
            ActiveCell.Offset(-1, 11).FormulaR1C1 = _
                    "=IF(AVERAGE(Table!R3C4:R20C4)>5,IF(AVERAGE(Table!R3C4:R20C4)<31,IF(INDIRECT(""b""&ROW())=0,"""",IF(INDIRECT(""q""&ROW())=""X"",IF(INDIRECT(""y""&ROW())=1,INDIRECT(""u""&ROW()),""""),"""")),""""),"""")"
            ActiveSheet.Protect DrawingObjects:=True, contents:=True, Scenarios:=True, AllowFiltering:=True
            
            Application.EnableEvents = True
        End If
    End Sub

  4. #4
    Registered User
    Join Date
    04-21-2009
    Location
    Nottingham
    MS-Off Ver
    Excel 2003
    Posts
    66

    Re: One macro prevents another from working

    Absolute star - thank you very much

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: One macro prevents another from working

    My pleasure! If that takes care of your need, be sure to EDIT your original post (Go Advanced) and mark the PREFIX box [SOLVED].



    (Also, use the "scales" icon across from the post dates to leave Feedback, it is appreciated)

+ 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