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! :banghead:

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.
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

morzapo

New Member
Joined
Aug 1, 2011
Messages
7
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,101,851
Messages
5,483,322
Members
407,393
Latest member
GeorgeBrown

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top