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.
by Allen Wyatt
(last updated August 25, 2021)
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.
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!
Excel allows you to fill a cell's background with just about any color you want. If you need to determine the RGB value ...Discover More
Excel allows you to add buttons to your worksheet that can be used to trigger macros. If you don't want those buttons to ...Discover More
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
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.