I'm working on a small project in Excel (Office 365) where I'm starting to lose my mind, since code is behaving crazy!
The idea is to open two windows of same Excel workbook at the same time, side-by-side, with specific window dimensions. I prepared an example of new workbook "Book1.xlsm" with two Worksheets ("Sheet1" and "Sheet2"). After pressing the "CommandButton1", there should be two windows of the Workbook placed on my monitor side by side with next properties:
I have two-monitor setup with screen resolution 1920×1200 each. You can see that I'm trying to place each window on one half of the monitor (2 × 960 px = 1920 px).
BUT(!) when the code is executed, the size and position of both windows are all WRONG:
Does anyone knows why is Excel ignoring size properties set in code and positions windows on these bizarre coordinates? Can someone please check the code and posts the results here? Thank you in advance!
Here is link to file Book1.xlsm in my Dropbox and here is also the code behind "CommandButton1":
Disclamer: since I'm quite of noob in VBA, I'm gathering code parts from all over the internet. The code above is modified code, found on Stack Overflow.
The idea is to open two windows of same Excel workbook at the same time, side-by-side, with specific window dimensions. I prepared an example of new workbook "Book1.xlsm" with two Worksheets ("Sheet1" and "Sheet2"). After pressing the "CommandButton1", there should be two windows of the Workbook placed on my monitor side by side with next properties:
- Left window showing "Sheet1" (Top=0, Left=0, Height=880, Width=960)
- Right window showing "Sheet2" (Top=0, Left=960, Height=880, Width=960)
I have two-monitor setup with screen resolution 1920×1200 each. You can see that I'm trying to place each window on one half of the monitor (2 × 960 px = 1920 px).
BUT(!) when the code is executed, the size and position of both windows are all WRONG:
- Left window showing "Sheet1" (Top=-2, Left=-2, Height=1173, Width=1280)
- Right window showing "Sheet2" (Top=-2, Left=1278, Height=1173, Width=1280)
Does anyone knows why is Excel ignoring size properties set in code and positions windows on these bizarre coordinates? Can someone please check the code and posts the results here? Thank you in advance!
Here is link to file Book1.xlsm in my Dropbox and here is also the code behind "CommandButton1":
Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
'Get rid of all open windows to start at 1. Easier than determining which windows are open and processing them.
Do Until Windows.Count = 1
Windows(Windows.Count).Close
Loop
Application.WindowState = xlMaximized
ActiveWindow.WindowState = xlMaximized
Application.WindowState = xlNormal
'Create 1 more for a total of 2 windows.
ActiveWindow.NewWindow
ActiveWindow.DisplayGridlines = False
ActiveWorkbook.Windows.Arrange ArrangeStyle:=xlVertical
For Each wnWin In Windows
Select Case wnWin.WindowNumber
'Right window: "Sheet2"
Case Is = 2
wnWin.Activate
Sheets("Sheet2").Select
With wnWin
.WindowState = xlNormal
.Top = 1
.Left = 960
.Height = 880
.Width = 960
.DisplayGridlines = False
.DisplayHeadings = False
End With
'Left window: "Sheet2"
Case Is = 1
wnWin.Activate
Sheets("Sheet1").Select
Sheets("Sheet2").Activate
With wnWin
.WindowState = xlNormal
.Top = 0
.Left = 0
.Height = 880
.Width = 960
.DisplayGridlines = False
.DisplayHeadings = False
End With
End Select
Next wnWin
Application.ScreenUpdating = True
End Sub
Disclamer: since I'm quite of noob in VBA, I'm gathering code parts from all over the internet. The code above is modified code, found on Stack Overflow.