Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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: Switching Windows in a Macro.

Switching Windows in a Macro

by Allen Wyatt
(last updated June 18, 2016)

1

Andrew needs to display a dialog box in his macro that allows a user to switch windows. When someone displays the View tab of the ribbon and clicks the Switch Windows tool, it shows the available workbooks to which the user can switch. 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11911) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Switching Windows 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

Error Generated if Files Opened in Word Session

Greg has a problem with Word 2002 crashing, depending on how he opens files. This seems to be a problem that Microsoft knows ...

Discover More

Too-Big Toolbars

Work around Words inability to have a multi-row toolbar.

Discover More

Comma-Delimited Differences for PC and Mac

When you choose to save worksheet data in CSV format, Excel gives you three choices for file formats. Those choices are ...

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 (ribbon)

Relative VBA Selections

Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here's the ...

Discover More

Setting Program Window Size in a Macro

The macro programming language used in Excel gives you a great many tools that allow you to modify the way that Excel appears ...

Discover More

Declaring Variables

Macros depend on the use of variables to do their work. This tip examines how variables are declared in a macro, using the ...

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}] 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 nine more than 9?

2016-06-18 06:57:01

Steve Askins

Allen - useful approach but I find it does not work if one of the open work books has more than one window open.

I get a Runtime error 13 and the debugger hightlights i=Val(v)

Any suggestions?

Steve
PS hope your son's wedding goes well!


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.