Prevent Code from Affecting a New Instance of Excel

Marhier

Board Regular
Joined
Feb 21, 2017
Messages
128
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi everyone, I hope you're well.
I've created a workbook that when opened, you're greeted with a dashboard of spliced pivot charts.
I've made it so when opened, it removes the following:

Scroll bars.
Formula Bar.
Headings.
Toolbar.
Ability to scroll.

The issue I've got is, I don't want it to affect a new instance of Excel, or if someone opens another workbook.

My code is as follows:

Code:
Private Sub Workbook_Open()
If ThisWorkbook.Name = "Purchasing Spend Source Data.xlsm" And ActiveWorkbook Is ThisWorkbook Then
    Dim ws As Worksheet
    Dim Sheet As Worksheet, Pivot As PivotTable
    Dashboard.Select
    For Each ws In ThisWorkbook.Sheets
        If ws.Name = "Dashboard" Then
            ws.Unprotect
            DisplayHorizontalScrollBar = False
            DisplayVerticalScrollBar = False
            Application.DisplayFormulaBar = False
            ActiveWindow.DisplayHeadings = False
            Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
            Sheets("Dashboard").ScrollArea = "A1"
            ws.Protect
        End If
    Next ws
    For Each Sheet In ThisWorkbook.Worksheets
    For Each Pivot In Sheet.PivotTables
        Pivot.RefreshTable
        Pivot.Update
    Next
    Next
End If
End Sub

I've tried to tackle this with the If ThisWorkbook.Name = - along with if it's the active workbook
Is what I'm trying to do even possible?

Any help would be greatly appreciated.

Thank you.
Regards
Marhier.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I use a similar scenario for recreating the right click menu. I am sure this can be adapted to reset everything you have changed.
just copy most of your above code and replace the False with true.

you need three bits of code.
On Close
On change focus - OFF
on change Focus - On

VBA Code:
Private Sub Workbook_Close()
'CODE TO RESET EVERYTHING YOU HAVE DONE
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
'CODE TO RESET EVERYTHING YOU HAVE DONE
End Sub

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
'CODE TO RESET EVERYTHING YOU HAVE DONE
End Sub
 
Upvote 0
Many thanks for the fast response.
I gave this a go, but if the code that hides everything is active, when I open a new instance of Excel, or another saved workbook, the effect still remains.
Only difference is now is if I close the new or other workbook, it brings everything back on the dashboard, lol.
 
Upvote 0
Have you got the code in the "This Work Book" section of "Purchasing Spend Source Data.xlsm"?
 
Upvote 0
OK, I think this should work
I guess you can leave your On open event as it is.

If you place this code in the "Dashboard" object

VBA Code:
Private Sub Worksheet_activate()
        ActiveSheet.Unprotect
        ActiveWindow.DisplayHorizontalScrollBar = False
        ActiveWindow.DisplayVerticalScrollBar = False
        ActiveWindow.Application.DisplayFormulaBar = False
        ActiveWindow.DisplayHeadings = False
        ActiveWindow.Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
        Sheets("Dashboard").ScrollArea = "A1"
        ActiveSheet.Protect
End Sub

Private Sub Worksheet_deactivate()
    ActiveSheet.Unprotect
    ActiveWindow.DisplayHorizontalScrollBar = True
    ActiveWindow.DisplayVerticalScrollBar = True
    ActiveWindow.Application.DisplayFormulaBar = True
    ActiveWindow.DisplayHeadings = True
    ActiveWindow.Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True)"
    ActiveSheet.Protect
End Sub

and this in the workbook objected
VBA Code:
Private Sub Workbook_activate()
        ActiveSheet.Unprotect
        ActiveWindow.DisplayHorizontalScrollBar = False
        ActiveWindow.DisplayVerticalScrollBar = False
        ActiveWindow.Application.DisplayFormulaBar = False
        ActiveWindow.DisplayHeadings = False
        ActiveWindow.Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
        Sheets("Dashboard").ScrollArea = "A1"
        ActiveSheet.Protect
End Sub

Private Sub Workbook_deactivate()
    ActiveSheet.Unprotect
    ActiveWindow.DisplayHorizontalScrollBar = True
    ActiveWindow.DisplayVerticalScrollBar = True
    ActiveWindow.Application.DisplayFormulaBar = True
    ActiveWindow.DisplayHeadings = True
    ActiveWindow.Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True)"
    ActiveSheet.Protect
End Sub
 
Upvote 0
Solution
adding
Application.ScreenUpdating = False
to the top of each line makes the screen update slightly less jerky, but you can still see the elements disappear.
 
Upvote 0
Oh, you absolute HERO!!!!
That worked a treat!

Legendary status confirmed.

Thank you!
 
Upvote 0
No problem.
feel like I have paid my dues. Came here looking for help and was glad I could help someone else while my question was being answered.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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