Shortening/combining VBA

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
Hello there, I want to assign keyboard shortcuts to save me time showing toolbars/unlocking worksheets and hiding toolbars/locking worksheets. I've created a VBA macro module called "Tools" and within in, two subs. It's not working at the moment and I think in part it's because I need to combine the code, but not sure exactly how to do this whilst ensuring it works.

For example, are "Sheet" and "Worksheet" considered the same thing by Excel VBA?

VBA Code:
Sub ProtectHide()
    Dim Sht As Sheet
        For Each Sht In ActiveWorkbook.Sheets
'        ActiveSheet.Protect "YourPassword"
        ActiveSheet.Protect
        Next Sht
    Dim wsSheet As Worksheet
    Application.ScreenUpdating = False
    For Each wsSheet In ThisWorkbook.Worksheets
        If Not wsSheet.Name = "Blank" Then
            wsSheet.Activate
            With ActiveWindow
                .DisplayFormulas = True
                .DisplayGridlines = False
                .DisplayHeadings = False
                .DisplayStatusBar = False
                .CommandBars("Full Screen").Visible = True
                .CommandBars("Worksheet Menu Bar").Enabled = False
                .CommandBars("Standard").Visible = False
                .CommandBars("Formatting").Visible = False
            End With
        End If
    Next wsSheet
    Application.ScreenUpdating = True
End Sub

Sub UnprotectShow()
    Dim Sht As Sheet
        For Each Sht In ActiveWorkbook.Sheets
'        ActiveSheet.Protect "YourPassword"
        ActiveSheet.Unprotect
        Next Sht
    Dim wsSheet As Worksheet
    Application.ScreenUpdating = False
    For Each wsSheet In ThisWorkbook.Worksheets
        If Not wsSheet.Name = "Blank" Then
            wsSheet.Activate
            With ActiveWindow
                .DisplayFormulas = True
                .DisplayGridlines = False
                .DisplayHeadings = True
                .DisplayStatusBar = True
                .CommandBars("Full Screen").Visible = True
                .CommandBars("Worksheet Menu Bar").Enabled = True
                .CommandBars("Standard").Visible = True
                .CommandBars("Formatting").Visible = True
            End With
        End If
    Next wsSheet
    Application.ScreenUpdating = True
End Sub

Thanks for your help.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top