VBA - Switching between Workbooks

jhol4

Board Regular
Joined
Nov 18, 2002
Messages
71
Hi Guys,

I posted a question earlier about using a RefEdit to link to external workbooks, however it seems that this cannot be done.

So my new question is does anyone have a good way of alternating between workbooks using VBA where the name of the workbook is likely to change regularly.

I usually use Windows(filename.xls).activate, however if I don't know what the filename is, this becomes difficult.

Thanks in advance.
 
This question came up earlier today. This macro would go into the first workbook, which allow you to find out the name of, and activate, the "other workbook".

Sub Test1()
Dim wb As Workbook, x As String
For Each wb In Workbooks
If wb.Name <> ThisWorkbook.Name Then x = wb.Name
Next wb
MsgBox "The other open workbook is named " & x & "." & vbCrLf & _
"Click OK to activate it."
Workbooks(x).Activate
End Sub
 
Upvote 0
Thanks Tom, do you know if there is a way for the user to select which workbook. The reason I am asking, is the process I am building is likely to have a number of workbooks open, and each workbook needs to be separately identified.
 
Upvote 0
If you want to list them it'd depend on the kind of user interface you prefer, and what your users would appreciate. A good way would be through a userform with a combobox that is populated at Initialization listing all the open workbooks in that instance of Excel, and prompting the user to select one from the drop-down. A more blue-collar approach would be a macro to list all open workbooks in column A for example, then ask the user to double click on the one they want to open. That runs the risk of someone closing an open workbook in between the listing and selection process. A userform is a lot cleaner, so all in all, that's what I'd recommend. Create one, and if you get stuck on any specific code post back so someone can assist.
 
Upvote 0
Hello Tom Urtis!

I have just registered because of your solution. I have been amazed about "jumping" to another Excel file that even you don't know the name. I would like to ask how are you doing this "multiple selection" possibility? I would be interested both possibilities, the "combo-box" and the "blue-collar" option.

Thank you very-very much!!!

Wow, now I have realized that this post is 8,5 years old! :)
 
Upvote 0
Responding to an IM and the post #6 Bat Signal...


Wow, this post goes back more than 7 years. Good for you to have done the preliminary searching. Here's a creative and easy way to activate other open workbooks, using a custom right click event menu.

Please follow these directions exactly as I write them.

Step 1
Start by closing all workbooks in Excel except for the workbook that you want this functionality for.

Step 2
In this lone open workbook, from any worksheet press Alt+F11 to go into the Visual Basic Editor. Careful, that's ALT + F11.

Step 3
Hit Ctrl+R.

Step 4
In the Project - VBA Project window that is in the vertical pane on the left, find your workbook name in bold font. It will look like VBAProject (YourWorkbookName.xls)

Step 5
Expand the yellow Objects folder.

Step 6
Right click on the object named ThisWorkbook, and left click to select ViewCode.

Step 7
Paste this code into the large white window pane that is the workbook module:

Code:
Private Sub Workbook_Open()
Run "RightKlickMenuMayker"
MsgBox "Right click any worksheet cell for a list of open workbooks" & vbCrLf & _
"that you can click to select and immediately navigate to.", 64, "Navigation tip"
End Sub
 
Private Sub Workbook_Activate()
Run "RightKlickMenuMayker"
End Sub
 
Private Sub Workbook_Deactivate()
Run "RightKlickMenuReeset"
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Run "RightKlickMenuReeset"
End Sub
 
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If Workbooks.Count = 1 Then Exit Sub
Cancel = True
Application.CommandBars("wbNavigator").ShowPopup
End Sub

Step 8
While you are in the VBE, from the menu bar at the top, click Insert > Module, and paste this code into that new module:

Code:
Private Sub RightKlickMenuReeset()
On Error Resume Next
Application.CommandBars("wbNavigator").Delete
Err.Clear
End Sub
 
Private Sub RightKlickMenuMayker()
Dim objBar As Object, objBtn As Object, i As Integer
Run "RightKlickMenuReeset"
Set objBar = Application.CommandBars.Add("wbNavigator", msoBarPopup)
For i = 1 To Workbooks.Count
Set objBtn = objBar.Controls.Add
With objBtn
.Caption = IIf(wb.Name = ThisWorkbook.Name, wb.Name & " (this workbook)", wb.Name)
.Style = msoButtonCaption
.OnAction = "wbActivayte"
End With
Next i
End Sub
 
Private Sub wbActivayte()
Workbooks(Application.Caller(1)).Activate
End Sub

Step 9
Press Alt+Q.

Step 10
Save your workbook.

Step 11
Close your workbook.

Step 12
Open your workbook and any other workbooks, and when your workbook is activate, from any worksheet, you can right click a cell to see a pop up list of other open workbooks. Left click to select the workbook name you want to activate.

Note that the custom right click menu will only appear if more than one workbook is open in that same instance of Excel, and it will only appear in the subject workbook where the code is housed.
 
Upvote 0
Hello Tom!

I have a runtime error, and tried to figure out what I can do with it, without any resolution:

It comes out with this line:

.Caption = IIf(wb.Name = ThisWorkbook.Name, wb.Name & " (this workbook)", wb.Name)

It says exactly:
Run-time error '424':
Object required

What do you think, how may I fix this?

Thank you very much Mr. Hardcore Excel Tom!!
 
Upvote 0
What do I think, you ask. I think I should have tested that more carefully.

Please substitute this macro, just take it out back and shoot it:

Code:
Private Sub RightKlickMenuMayker()
Dim objBar As Object, objBtn As Object, i As Integer
Run "RightKlickMenuReeset"
Set objBar = Application.CommandBars.Add("wbNavigator", msoBarPopup)
For i = 1 To Workbooks.Count
Set objBtn = objBar.Controls.Add
With objBtn
.Caption = IIf(wb.Name = ThisWorkbook.Name, wb.Name & " (this workbook)", wb.Name)
.Style = msoButtonCaption
.OnAction = "wbActivayte"
End With
Next i
End Sub


and put this one in its place:

Code:
Private Sub RightKlickMenuMayker()
Dim objBar As Object, objBtn As Object, wb As Workbook
Run "RightKlickMenuReeset"
Set objBar = Application.CommandBars.Add("wbNavigator", msoBarPopup)
For Each wb In Workbooks
Set objBtn = objBar.Controls.Add
With objBtn
.Caption = IIf(wb.Name = ThisWorkbook.Name, wb.Name & " (this workbook)", wb.Name)
.Style = msoButtonCaption
.OnAction = "wbActivayte"
End With
Next wb
End Sub


Then save the workbook and open a few other workbooks, go back to that subject workbook, right click on any worksheet cell, and it should be OK. Definitley let me know if you run into problems.
 
Upvote 0
Hello!

I must say that you are the biggest professional I have ever seen in macro :)
Thank you a lot! I hope one day I can also help some guys like you did it to me.

Hail to the master! :bow:
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top