Log in

View Full Version : Excel 2013: How To Check How Many Excel Files Are Open



peccora3
06-08-2013, 03:08 AM
Hello,

In previous Excel vba, I used "application.windows.count" to check how many Excel files are open.
However, in Excel 2013, this script returns 1 no matter how many files are open.
What vba script can I use to determine how many Excel files are open in Excel 2013?

Excel Fox
06-08-2013, 04:13 AM
Try
application.workbooks.Count
Or

Sub CountOpenWorkbooks()

Dim wbk As Workbook
Dim lng As Long

For Each wbk In Workbooks
If Not wbk.IsAddin Then
lng = lng + 1
End If
Next wbk
MsgBox "There are " & lng & " workbooks open", vbOKOnly, ""
End Sub

peccora3
06-08-2013, 05:53 AM
Thank you for your quick answer.
Your script works fine in Excel vba.
Now, I need to run this script from Powerpoint, and either of the scripts returns 0.

Following is what I have written for your first answer:
(I have Microsoft Excel 15.0 Object Library checked on Reference and a few Excel files open prior to running the script)


Dim exl as Excel.Application
Dim wbct as integer
Set exl = New Excel.Application
exl.Visible = True
wbct = exl.Application.Workbooks.Count '<= wbct returns 0

Your second answer returned the same.

What am I doing wrong?

Admin
06-08-2013, 08:51 AM
Hi

try


Dim appExcel As Excel.Application
Dim Wbk As Excel.Workbook
Dim lngCount As Long

On Error Resume Next
Set appExcel = GetObject(, "Excel.Application")
On Error GoTo 0

If appExcel Is Nothing Then
MsgBox "There are no workbooks open", vbOKOnly
Exit Sub
Else
For Each Wbk In appExcel.Workbooks
If Not Wbk.IsAddin Then
lngCount = lngCount + 1
End If
Next

MsgBox "There are " & lngCount & " workbooks open", vbOKOnly
End If

peccora3
06-09-2013, 01:43 AM
This worked perfectly.
Tank you very much!