Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Setting Program Window Size in a Macro.

Setting Program Window Size in a Macro

Written by Allen Wyatt (last updated April 26, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


3

Christopher needs, within a macro, to set the size of the Excel program window. He knows how to set the size of a worksheet within the program window, but that isn't what he needs. He wonders how he can set the overall size of the program window, plus make sure that he doesn't set it larger than the user's actual screen size.

This can be done rather easily if one knows which objects and properties to use in your macro. The object you want to use is the Application object, which refers to the Excel application. Here are the pertinent properties:

  • Top. The screen pixel at which the top edge of the application window should be placed.
  • Left. The screen pixel at which the left edge of the application window should be placed.
  • Width. The width of the application window, in pixels.
  • Height. The height of the application window, in pixels.

With these in mind, you could set the position and size of the program window in this manner:

Sub SetWindowSize1()
    Application.WindowState = xlNormal
    Application.Top = 25
    Application.Left = 25
    Application.Width = 300
    Application.Height = 200
End Sub

This macro specifies the upper-left corner of the program window to be 25 pixels from the top of the screen and 25 pixels from the left of the screen. Then, the program window is set to be 300 pixels wide and 200 pixels tall. Note, as well, the setting of the WindowState property at the first of the macro. This sets the window to be in a "normal" state, meaning one that can be resized to something larger than minimized and smaller than maximized. (If you want the Excel program window to take their entire screen, simply set the WindowState property to xlMaximized and forget the rest of the settings in the macro.)

Of course, this macro sets the Excel program window to be rather small. In all likelihood you'll want it to be larger, but you don't want it to be larger than the size of the user's screen. The easiest way to figure out the size of the user's screen is to simply maximize the Excel application window and then look at the Width and Height properties. You can then adjust those figures based on where you want the upper-left corner of the screen to be and then adjust accordingly.

As an example, let's say that you want the program window to start at 25, 50 and you want it to be 1000 x 500. You could use code similar to the following:

Sub SetWindowSize2()
    Dim iMaxWidth As Integer
    Dim iMaxHeight As Integer
    Dim iStartX As Integer
    Dim iStartY As Integer
    Dim iDesiredWidth As Integer
    Dim iDesiredHeight As Integer

    iStartX = 50      ' Distance from left
    iStartY = 25      ' Distance from top
    iDesiredWidth = 1000
    iDesiredHeight = 500

    With Application
        .WindowState = xlMaximized
        iMaxWidth = Application.Width
        iMaxHeight = Application.Height

        ' Adjust for starting point
        iMaxWidth = iMaxWidth - iStartX
        iMaxHeight = iMaxHeight - iStartY
        If iDesiredWidth > iMaxWidth Then
            iDesiredWidth = iMaxWidth
        End If
        If iDesiredHeight > iMaxHeight Then
            iDesiredHeight = iMaxHeight
        End If

        .WindowState = xlNormal
        .Top = iStartY
        .Left = iStartX
        .Width = iDesiredWidth
        .Height = iDesiredHeight
    End With
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10939) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Setting Program Window Size in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Specifying the Number of MRU Files

MRU (most recently used) files can be a great help when you work with a given set of common workbooks. Excel allows you ...

Discover More

Repeating Table Rows with Manual Page Breaks

Need to make sure part of a table is on one page and part on another? The way to do so is not to use manual page breaks, ...

Discover More

Disappearing Ribbon Buttons

Excel allows you to configure your system, even to the point of adding macros to your ribbon area. What are you to do, ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Understanding the Select Case Structure

One of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...

Discover More

Clearing the Clipboard in a Macro

You may want your macro to clear the clipboard so that people cannot access anything left in the clipboard. That is ...

Discover More

Ctrl+Break Won't Work to Stop a Macro

When you need to stop a macro while it is running, you normally press Ctrl+Break. What are you to do if the keypress ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three less than 3?

2021-05-07 05:27:09

Alex

Hello Sir,
The code is pretty good if you find a way to set the bottom and right side that will be an answer for a lot of VBA users.
Kind Regards,
Alex


2020-11-02 09:55:42

J. Woolley

@John Mann
The macros in this Tip (and similar macros) are intended to be run manually (Alt+F8). Such macros should be placed in a standard module in your VBAProject (Alt+F11, then Insert > Module). If you want to run one of these macros (say SetWindowSize1) automatically whenever the Workbook is opened, copy this VBA and paste it into your VBAProject's ThisWorkbook module:
Private Sub Workbook_Open()
SetWindowSize1
End Sub


2020-11-01 15:43:11

John Mann

Nice tip to find. I've just been wondering about doing exactly what this tip provides for - except I'm not clear on where to place the macro to set the size. Would the proper place be "ThiWorkbook" in the project list, followed by "Workbook" in the module window?

What I've been thinking about is having workbooks which open to a size that's suited to the purpose. Some of my workbooks are best run maximized, while others can be run at a smaller size, especially the width. I prefer not to have a large amount of unused space in my application windows.

I would also appreciate some pointes on how to note the size of the window when I close the workbook, so that next time it opens to the last used size. That would be a nice refinement.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.