workbooks.count apparently miscounting

User avatar
stuck
Panoramic Lounger
Posts: 8160
Joined: 25 Jan 2010, 09:09
Location: retirement

workbooks.count apparently miscounting

Post by stuck »

If I have Excel open but no workbooks open then my code (In Word VBA module) that says:

Code: Select all

numBks = xlApp.Workbooks.Count
shows numbks to be '1'.

Would that be because the .Count is the count of ALL workbooks so it's counting personal.xls or some other hidden workbook?

Ken

User avatar
HansV
Administrator
Posts: 78402
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: workbooks.count apparently miscounting

Post by HansV »

The Workbooks collection consists of all open workbooks, whether visible or hidden.
So Personal.xls(b) is included in the Workbooks collection.
Add-ins are *not* included.
Best wishes,
Hans

User avatar
stuck
Panoramic Lounger
Posts: 8160
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: workbooks.count apparently miscounting

Post by stuck »

Thanks for the confirmation.
:thumbup:

Ken

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: workbooks.count apparently miscounting

Post by Jan Karel Pieterse »

I tend to use this function:

Code: Select all

Function CountVisible() As Boolean
    Dim lCt As Long
    Dim oWb As Workbook
    For Each oWb in Workbooks
        If oWb.Windows(1).Visible = True Then
            lCt = lCt + 1
        End If
    Next
    CountVisible = lCt
End Function
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com