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.
Note:
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!
Variable arrays are used quite often in macros. If you use an array once in your macro and then need to reuse it for ...
Discover MoreVBA provides a few different ways you can search for information within strings. This tip looks at the most efficient ...
Discover MoreNeed to figure out an absolute value within your macro code? It's easy to do using the Abs function, described in this tip.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-12-16 15:16:20
J. Woolley
Instead of switching windows with a macro, it might be better to switch workbooks:
Sub SwitchWorkbooks()
Dim i As Integer
Dim n As Integer
Dim s As String
Dim v As Variant
n = Workbooks.Count
s = "Choose Workbook from:"
For i = 1 To n
s = s & Chr(10) & i & ") " & Workbooks(i).Name
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
Workbooks(i).Activate
End If
End Sub
2021-12-14 12:08:11
BJ Thomas
Hello and thanks for such great tips! I also received an error in Office 365 - is this version restricted or am I the actual issue? Thanks! BJ
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!
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments