Switching Windows in a Macro

Written by Allen Wyatt (last updated January 13, 2024)
This tip applies to Excel 97, 2000, 2002, and 2003


Andrew needs, in his macro, to display a dialog box that allows a user to switch windows. When someone clicks the Window menu (in Excel), the available workbooks are listed at the bottom of the menu. Those are what Andrew needs to show up in the dialog box. He wonders if there is a built-in dialog box to do this, or if he needs to create his own.

The short answer is that there is no built-in dialog box to accomplish this task. You can, however, easily create your own. Here is a simple example:

Sub SwitchWindows()
    Dim i As Integer
    Dim n As Integer
    Dim s As String
    Dim v As Variant

    n = Windows.Count
    s = "Choose Window from:"
    For i = 1 To n
        s = s & Chr(10) & i & ")  " & Windows(i).Caption
    Next
    s = s & Chr(10) & "Enter a number from 1 to " & n
    v = Application.InputBox(prompt:=s, Type:=2)
    i = Val(v)
    If i >= 1 And i <= n Then
        Windows(i).Activate
    End If
End Sub

All this does is create a list of the names for each window in your system. It presents them in an InputBox, and then switches to whatever window the user selected.

If you are seeking different ways to present the same information, you can refer to this tip.

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 (11910) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Getting Rid of Mail Merge Section Breaks

When you create a group of documents from a merge file, Word normally inserts section breaks between iterations of the ...

Discover More

Sorting by Headings

Headings are a great way to organize your document. If, after getting your headings in place, you want to sort by those ...

Discover More

Forcing Dates Forward

Want to push a date to some pre-defined day of the month? Here are some ways to force the issue.

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's ...

Discover More

Error Using ATAN2 Function in Macro

Excel allows you to use worksheet functions from within macros. This is helpful, especially when you are trying to ...

Discover More

Assigning a Macro to a Keyboard Combination

Macros are a great way to expand what you can do with the data you place in Excel. You can make those macros even handier ...

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 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.