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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
When working with colors in Excel you can specify them using either RGB or HSL values. Converting from HSL to RGB can be ...
Discover MoreWhen creating macros, it is often necessary to change from one type of data to another. Here's how you can change from a ...
Discover MoreAs your macro processes information in a worksheet, you may want to make sure that it skips over rows that are hidden. ...
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