Switch Between Workbooks

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to toggle between two different open workbooks (these two would be the
only open visible workbooks at the time). The problem is that I don't always
know what the name of the workbooks will be. Could someone please help with
some code that would take the current active workbook, designate that
workbook as WB1, and then designate the other open workbook as WB2. In the
end, I want to be able to switch back and forth through the two as follows:

WB1.Activate
WB2.Activate

Thanks.
 
Jason,

try something like this. it will only work if there are only 2 open
workbooks.

Sub test()
Dim wb1 As Workbook
Dim wb2 As Workbook

If Workbooks.Count = 2 Then
Set wb1 = ActiveWorkbook
Set wb2 = IIf(Workbooks(1).Name = wb1.Name, Workbooks(2), Workbooks(1))
End If

End Sub
 
Vergel,

This is probably close to what I need, however, the problem is that I have
open the personal.xls folder (which is actually where this code will be
stored). Personal.xls is always a hidden workbook, but it is open
nonetheless. Is there someway to workaround this so it only considers the
open and visible workbooks.

Thanks.
 
Jason,

Maybe something like this then... it assigns the activeworkbook to wb1 and
then loops through all workbooks to find the next one that is not equal to
the activeworkbook and personal workbook..


Sub test()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim wb As Workbook

Set wb1 = ActiveWorkbook
For Each wb In Workbooks
If wb.Name <> wb1.Name And UCase(wb.Name) <> "PERSONAL.XLS" Then
Set wb2 = wb
Set wb = Nothing
Exit For
End If
Next wb

End Sub
 
Hi Jason,

How is everything going? Please feel free to let me know if you need any
assistance.

Sincerely,

Wei Lu
Microsoft Online Community Support

==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top