Help with a quick way to close all duplicate windows using VBA please

W

willjohnson33

I am trying to figure out a way to close all duplicate windows of
workbooks using VBA
I thought this would work but it doesn't.

For Each wb In Workbooks
If Windows.Count > 1 Then
For i = 1 To Windows.Count
Windows(2).Close
Next i
End If
Next wb

For example: I have 3 workbooks wb1, wb2, wb3 and with wb3 I have 2
windows open so there are acutally 4 windows total. wb1, wb2, wb3:1,
wb3:2

I only want to close wb3:2. And would prefer to do it without having
to actually activate each workbook.

Any help is greatly appreciated.
 
D

Dave Peterson

Maybe...

Dim iCtr As Long
Dim wb As Workbook

For Each wb In Application.Workbooks
For iCtr = wb.Windows.Count To 2 Step -1
wb.Windows(iCtr).Close
Next iCtr
Next wb
 
W

willjohnson33

Worked great - thanks for the help.


Dave said:
Maybe...

Dim iCtr As Long
Dim wb As Workbook

For Each wb In Application.Workbooks
For iCtr = wb.Windows.Count To 2 Step -1
wb.Windows(iCtr).Close
Next iCtr
Next wb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top