Adding additional code to a workbook_open event

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Adding additional code to a workbook_open event

Post by ABabeNChrist »

I’m using a workbook_open event to check for a certain value in a selected range of a specified sheet. If value is not present then a UserForm named Activation will open. This all works well. Code below

Code: Select all

Private Sub workbook_open()

    If Sheets("Comments").Range("CR2") = "HomeInspection" Then
        UserForm1.Show
        Dim wsh As Worksheet
        For Each wsh In Worksheets
            wsh.Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
                        Password:="", UserInterFaceOnly:=True
        Next wsh
        ActiveWorkbook.Protect Password:="benji", Structure:=True, Windows:=True
        Exit Sub
    End If
    Activation.Show

End Sub
I now wanted to add another piece of code that would make 1 of 2 buttons visible true or visible false and this would be determined by name of workbook. Both buttons are in same location, just stacked on top of one another. It seems to work Ok; I just want to make sure I’m using the correct approach. Sample below

Code: Select all

Private Sub workbook_open()

    If ActiveWorkbook.Name <> "Master.xlsm" Then
        
        Sheets("Client_info").UpdateLog.Visible = True
        Sheets("Client_info").ViewLog.Visible = False

    Else
        
        Sheets("Client_info").UpdateLog.Visible = False
        Sheets("Client_info").ViewLog.Visible = True

    End If

    If Sheets("Comments").Range("CR2") = "HomeInspection" Then
        UserForm1.Show
        Dim wsh As Worksheet
        For Each wsh In Worksheets
            wsh.Protect DrawingObjects:=False, Contents:=True, Scenarios:=True, _
                        Password:="", UserInterFaceOnly:=True
        Next wsh
        ActiveWorkbook.Protect Password:="benji", Structure:=True, Windows:=True
        Exit Sub
    End If
    Activation.Show

End Sub

User avatar
HansV
Administrator
Posts: 78534
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Adding additional code to a workbook_open event

Post by HansV »

That looks OK. If you wish, you can shorten the code

Code: Select all

    If ActiveWorkbook.Name <> "Master.xlsm" Then
        
        Sheets("Client_info").UpdateLog.Visible = True
        Sheets("Client_info").ViewLog.Visible = False

    Else
        
        Sheets("Client_info").UpdateLog.Visible = False
        Sheets("Client_info").ViewLog.Visible = True

    End If
to

Code: Select all

    With Sheets("Client_info")
        .UpdateLog.Visible = (ActiveWorkbook.Name <> "Master.xlsm")
        .ViewLog.Visible = (ActiveWorkbook.Name = "Master.xlsm")
    End With
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Adding additional code to a workbook_open event

Post by ABabeNChrist »

Thank You Hans