Link to home
Create AccountLog in
Avatar of route217
route217Flag for United Kingdom of Great Britain and Northern Ireland

asked on

One macro is prevent another from running......

Hi Experts

i have the following code which works prefectly for what it need to do.....

See Code:

Option Explicit

Private Sub Workbook_Activate()
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim MySht As Worksheet

    Set MySht = ActiveSheet
    With Application
        .ScreenUpdating = False
        .ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",False)"
        .DisplayFormulaBar = False
        .DisplayStatusBar = Not Application.DisplayStatusBar
    End With
    ActiveWindow.DisplayWorkbookTabs = False


    Set wbBook = ThisWorkbook

    For Each wsSheet In wbBook.Worksheets
        With wsSheet
            If Not .Name = "Blank" Then .Activate
            With ActiveWindow
                .DisplayHeadings = False
                .DisplayGridlines = False
            End With
            .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingRows:=True
        End With
    Next wsSheet

    MySht.Select

End Sub

Private Sub Workbook_Deactivate()
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim MySht As Worksheet

    Set MySht = ActiveSheet
    With Application
        .ScreenUpdating = False
        .ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",True)"
        .DisplayFormulaBar = True
        .DisplayStatusBar = True
    End With
    ActiveWindow.DisplayWorkbookTabs = True


    Set wbBook = ThisWorkbook

    For Each wsSheet In wbBook.Worksheets
        With wsSheet
            If Not .Name = "Blank" Then .Activate
            With ActiveWindow
                .DisplayHeadings = True
                .DisplayGridlines = True
            End With
            .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFormattingRows:=True
        End With
    Next wsSheet

''RB: I have commented the below code out because (in theory - untested) this prevents the workbook from being deactivated (in fact, even the ".Activate" in the previous section may prevent deactivation.
'    MySht.Select
End Sub

Open in new window


But this code is prevent the following macro from running and openning up worksheet "Source Data"...which i need to work.....also

See Code....
Option Explicit

Sub ToggleVisibilityOfDataWs()
Const MasterName As String = "Master"
Const pw As String = "secret"

    With ThisWorkbook
        If .ActiveSheet.Name = "Master" Then
            With .Worksheets("Source Data")
                If .Visible Then
                    .Visible = False
                Else
                    If InputBox("Please enter password to show Source Data worksheet:", "ENTER PASSWORD...") = pw Then
                        .Visible = True
                    Else
                        MsgBox "Password incorrect!", vbOKOnly + vbInformation, "Macro Feedback..."
                    End If
                End If
            End With
        Else
            MsgBox "Can only run this macro from the worksheet called " & MasterName, vbOKOnly + vbInformation, "Macro Feedback..."
        End If
    End With
End Sub

Open in new window

Avatar of Shanan212
Shanan212
Flag of Canada image

Option Explicit

Sub ToggleVisibilityOfDataWs()
application.EnableEvents = false
Const MasterName As String = "Master"
Const pw As String = "secret"

    With ThisWorkbook
        If .ActiveSheet.Name = "Master" Then
            With .Worksheets("Source Data")
                If .Visible Then
                    .Visible = False
                Else
                    If InputBox("Please enter password to show Source Data worksheet:", "ENTER PASSWORD...") = pw Then
                        .Visible = True
                    Else
                        MsgBox "Password incorrect!", vbOKOnly + vbInformation, "Macro Feedback..."
                    End If
                End If
            End With
        Else
            MsgBox "Can only run this macro from the worksheet called " & MasterName, vbOKOnly + vbInformation, "Macro Feedback..."
        End If
    End With
application.EnableEvents = true
End Sub

Open in new window


Try now.

I've added application.EnableEvents = false to disable the listener
Avatar of route217

ASKER

hi expert

I have changed the code as above and still cannot get the sheet to display. ..
ok I have added...

activewindow.displayworkbooktavs = True...

to the end of the code..
How would you know it is preventing? Is it displaying error messages?
no worksheet tabs are visible on worksheet. ..
ASKER CERTIFIED SOLUTION
Avatar of Shanan212
Shanan212
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
excellent work. ..perfectly. .
shanan212

great feedback...can you kindly cast your eye over https://www.experts-exchange.com/questions/28174915/use-uerform-to-validated-data-in-source-sheet.html

regards