How do you loop through multiple Excel "windows"

biocentrism

Board Regular
Joined
Jul 28, 2014
Messages
187
Under View-->Window-->New Window you can open a new "window" of the workbook you are working on. This adds a " - 1" and a " -2" to the name of each window in the upper bar header.

I am trying to figure out how to loop through each of the windows using the VBA.

For instance if my Workbook is called "Book1.xlsm" and I have 2 Windows displayed showing this workbook. And then I want to go to Window1 and activate Sheet1. And then I want to go to Window2 and activate Sheet2.

How would I write the code for this?

Thanks.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
In the Excel version I use (2013) the title bar's suffix of a duplicated window is a colon and a number like "book.xlsm:2". If your version displays it otherwise, you're supposed to adjust the code below. Anyway, this should do what you're after, provided both Sheet1 and Sheet2 exist.

VBA Code:
Sub biocentrism()
    With ThisWorkbook.Windows
        If .Count > 1 Then
            .Item(.Parent.Name & ":1").Activate
            .Parent.Worksheets("Sheet1").Activate
            .Item(.Parent.Name & ":2").Activate
            .Parent.Worksheets("Sheet2").Activate
        End If
    End With
End Sub
 
Upvote 0
I am looking for how to handle after 2013. Version 365 does not use the "book.xlsm:2" convention anymore.
 
Upvote 0
If your version displays it otherwise, you're supposed to adjust the code below.

So perhaps something like this (note the red colored amendments):
Rich (BB code):
Sub biocentrism()
    With ThisWorkbook.Windows
        If .Count > 1 Then
            .Item(.Parent.Name & "-1").Activate
            .Parent.Worksheets("Sheet1").Activate
            .Item(.Parent.Name & "-2").Activate
            .Parent.Worksheets("Sheet2").Activate
        End If
    End With
End Sub

or ...
Rich (BB code):
Sub biocentrism()
    With ThisWorkbook.Windows
        If .Count > 1 Then
            .Item(.Parent.Name & " - 1").Activate
            .Parent.Worksheets("Sheet1").Activate
            .Item(.Parent.Name & " - 2").Activate
            .Parent.Worksheets("Sheet2").Activate
        End If
    End With
End Sub
 
Upvote 0
actually i just figured it out. tricky. so there are double spaces before and after the hyphen. so its:

.Item(.Parent.Name & " - 1").Activate

thanks both for your help!
 
Upvote 0
... as suggested in the second example in my previous post. Glad it's sorted and thanks for letting me know.
just to make sure others understand the syntax requires double spaces before and after the hyphen to work not just single space.
 
Upvote 0
Pasting your example in a text editor gave me [space] [hyphen] [space]
So if I understand correctly it should be: [space] [space] [hyphen] [space] [space]
which means this site has trimmed your text.
Thanks for replying on that (y)
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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