Open Workbook Empty?

  • Thread starter Thread starter Nigel RS
  • Start date Start date
N

Nigel RS

Is there a simple way to determine if an open workbook has any data?

Maybe the used range = nothing?
 
Hi Nigel,

Try this, from an other workbook using the name of the workbook you
want to check as parameter

Public Function IsWorkBookEmpty(aWorkbook As Workbook) As Boolean
Dim sht As Worksheet
Dim bln As Boolean

bln = True
For Each sht In aWorkbook.Sheets
If sht.UsedRange.Cells.Count > 1 Then
bln = False
Exit For
Else
If Not IsEmpty(sht.Range("A1")) Then
bln = False
Exit For
End If
End If
End If

If bln Then
If aWorkbook.Charts.Count > 0 Then
bln = False
Exit For
End If
End If
End Function


HTH,

RadarEye
 
Thanks with modification it works ok.

I had to assign the bln to the function name to get the returned value,
change the usedrange count logical test to >0 not >1, fix the for next loop
and remove the extra code in the charts count area. So I ended up with

Public Function IsWorkBookEmpty(aWorkbook As Workbook) As Boolean
Dim sht As Worksheet
Dim bln As Boolean

bln = True
For Each sht In aWorkbook.Sheets
If sht.UsedRange.Cells.Count > 0 Then
bln = False
Exit For
Else
If Not IsEmpty(sht.Range("A1")) Then
bln = False
Exit For
End If
End If
Next
If bln And aWorkbook.Charts.Count > 0 Then bln = False
IsWorkBookEmpty = bln
End Function
 

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

Back
Top