Macro to configure screen when file opens and closes

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
773
Office Version
  1. 365
Platform
  1. Windows
I've been using the codes below on buttons to toggle back and forth between a split screen (with scrollbars) and a full screen.

Instead of the buttons and I'd like to change it so that when the file opens it automatically runs the SPLIT SCREEN macro AND when the user closes either of the two screens/panes it automatically runs the FULL SCREEN macro and closes the entire file (whether the user chooses to save or not). Can someone help with this?

FULL SCREEN

Code:
Sub FullScreen()
Application.ScreenUpdating = False
    ActiveWindow.Close
    ActiveWindow.WindowState = xlMaximized
Application.Goto Range("A1")
End Sub

SPLIT SCREEN

Code:
Sub SplitScreen()
Application.ScreenUpdating = False
Dim wd As Double, ht As Double
 
    wd = Application.UsableWidth
    ht = Application.UsableHeight
 
    With ActiveWindow
        .WindowState = xlNormal
        .Left = 0
        .Top = 0
        .Width = wd * 1 / 2
        .Height = ht
        Application.Goto Range("A1"), Scroll:=True
    End With
 
    With ActiveWindow.NewWindow
        .Left = wd * 1 / 2
        .Top = 0
        .Width = wd * 1 / 2
        .Height = ht
        Application.Goto Range("AA1"), Scroll:=True
    End With
Application.ScreenUpdating = True
End Sub
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Should be just a case of running your macros when the approate events are tiggered.
Code:
Private Sub Workbook_Open()
    Run "SplitScreen"
End Sub

Private Sub Workbook_WindowResize(ByVal Wn As Excel.Window)
    Run "FullScreen"
End Sub

That code has to go into the 'ThisWorkbook' section (In the VBA Editor) double click 'ThisWorkbook' and change the section from (General) to Workbook in the drop down box just above where you type your code.

Oh as as you are making macros run when you open a workbook. If you want to open a workbook without the Workbook_Open() running.
Just hold down shift key when you open the excel sheet from the File->Open dialog or most recently used list.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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