Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel open workbook check

Status
Not open for further replies.

Kenny1943

MIS
Jul 18, 2003
13
US
Hello,

I have several excel workbooks i am trying to save with sequencial file names i.e. file1, file2, file3...

All the workbooks are open and I using the Saveas method to save them, then closing the workbook before moving on to the next.

How can I write my macro to stop once it has closed all worbooks.
 
Have a search in the VBE help on 'Do...Loop' or 'For...Next'

HTH
 
Hi Kenny1943,

This is not quite as easy as you might think.

To simply loop through all the open workbooks, use something like:

Code:
Dim WB As Workbook
For Each WB in Application.Workbooks
Code:
    ' Do your stuff here
Code:
Next

But, once you close the workbook containing your code it will stop running so you must ensure it is the last one closed. You could do this ..

Code:
Dim WB As Workbook
For Each WB in Application.Workbooks
    If WB.Name <> ThisWorkbook.Name then
Code:
        ' Do your stuff here
Code:
    End If
Next
ThisWorkbook.Close

Or, somewhat better I would suggest, you could put your code in your Personal.xls Workbook. This then raises the problem of it being an open workbook which you do not want to SaveAs and Close. There may also be other open workbooks (e.g. Addins) which you do not actually want to process but all of these are in hidden windows so one solution would be ..

Code:
Dim WB As Workbook
For Each WB in Application.Workbooks
    If WB.Windows(1).Visible then
Code:
        ' Do your stuff here
Code:
    End If
Next

Enjoy,
Tony
 
Tony,


Thanks for the quick response. This seems to work if there are no hidden windows on a spreadsheet. I tried your last suggestion because I would like to keep this in my Personal.xls file. It seems because some of the spreadsheets have hidden windows, it's not saving all the spreadsheet I have open.

Here's the code I wrote:

Sub TestMacro()
Dim WB As Workbook
Dim i As Integer

i = 0
For Each WB In Application.Workbooks
If WB.Windows(1).Visible Then
i = i + 1
ActiveWorkbook.SaveAs &quot;C:\File&quot; & i
ActiveWorkbook.Close False
End If
Next

End Sub
 
Hi Kenny1943,

If that's the case, I would try checking for those with any visible windows which is a bit more complex. This should do it:

Code:
Sub TestMacro()
    Dim WB As Workbook
    Dim i As Integer
Code:
Dim Win As Window
    Dim TotallyHidden as Boolean
Code:
    i = 0
    For Each WB In Application.Workbooks
Code:
TotallyHidden = True
        For Each Win In WB.Windows
            If Win.Visible Then TotallyHidden = False
        Next
        If Not TotallyHidden Then
Code:
Code:
' This line replaced: If WB.Windows(1).Visible Then
Code:
            i = i + 1
            ActiveWorkbook.SaveAs &quot;C:\File&quot; & i
            ActiveWorkbook.Close False
        End If
    Next
    
End Sub

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top