Create New Excel Window - size and position are TOTALY WRONG!

morzapo

New Member
Joined
Aug 1, 2011
Messages
7
I'm working on a small project in Excel (Office 365) where I'm starting to lose my mind, since code is behaving crazy! :oops:

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.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
To make shorter version of my original post: it is maybe easier if I make a poll here:

Please test button in this file and post the result. I hope you can estimate the size of both windows ...

Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,356
Members
448,888
Latest member
Arle8907

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