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

Designing Standard Tables

If you have a common table layout that you want to use again and again, you'd benefit by having an easy way to save that ...

Discover More

Getting Bookmark Information in VBA

Bookmarks are a handy way to "mark" locations within a document. If you are creating a macro that processes the document in ...

Discover More

Copying Headers and Footers

Need to get headers and footers from one document to another? You can use the steps in this tip to help make quick work of ...

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)

Determining the Hour of the Day

Need to know the current hour of the day? You can derive the information in your macros by using the Hour function, as ...

Discover More

Trouble Recording Paste Special Formula

Sometimes, when you upgrade to a new version of Excel, you could run into a problem recording macros that you had no problem ...

Discover More

Finding Workbooks Containing Macros

Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might want ...

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 for this tip:

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. 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 7 + 8?

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!


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.

Links and Sharing
Share