PDA

View Full Version : Solved: Check if sheet is blank



Djblois
02-28-2007, 08:43 AM
I would like to cheak if a sheet is blank or not and the run code on the sheet if it isn't blank. I know how to check if a cell is blank but not a whole worksheet.

lucas
02-28-2007, 08:51 AM
I think something like this will work Daniel:
For Each WS In Wkb.Worksheets
Set LastCell = WS.Cells.SpecialCells(xlCellTypeLastCell)
If LastCell.Value = "" And LastCell.Address = Range("$A$1").Address Then
Else

Djblois
02-28-2007, 09:00 AM
Lucas

that worked perfectly thank you

lucas
02-28-2007, 09:03 AM
Yoinked it from Joseph(Malik641)
I use it with his combine workbooks code excluding blank sheets.

mdmackillop
02-28-2007, 11:47 AM
... or

Sub Blank()
If Application.WorksheetFunction.CountBlank(ActiveSheet.Cells) = _
Rows.Count * Columns.Count Then
MsgBox "sheet is blank"
End If
End Sub

mdmackillop
02-28-2007, 11:57 AM
BTW, this is probably better carried out as a function. You can use either version of the code to check.


Sub TestBlanks()
Dim sh As Worksheet
For Each sh In Worksheets
If IsBlank(sh) Then MsgBox sh.Name & " is blank"
Next
'or
For Each sh In Worksheets
If IsBlank2(sh) Then MsgBox sh.Name & " is blank"
Next
End Sub

Function IsBlank(sh As Worksheet)
If Application.WorksheetFunction.CountBlank(sh.Cells) = _
Rows.Count * Columns.Count Then
IsBlank = True
End If
End Function

Function IsBlank2(sh As Worksheet)
Set LastCell = sh.Cells.SpecialCells(xlCellTypeLastCell)
If LastCell.Value = "" And LastCell.Address = Range("$A$1").Address Then IsBlank2 = True
End Function

xeterson
01-16-2009, 03:07 PM
BTW, this is probably better carried out as a function. You can use either version of the code to check.


Sub TestBlanks()
Dim sh As Worksheet
For Each sh In Worksheets
If IsBlank(sh) Then MsgBox sh.Name & " is blank"
Next
'or
For Each sh In Worksheets
If IsBlank2(sh) Then MsgBox sh.Name & " is blank"
Next
End Sub

Function IsBlank(sh As Worksheet)
If Application.WorksheetFunction.CountBlank(sh.Cells) = _
Rows.Count * Columns.Count Then
IsBlank = True
End If
End Function

Function IsBlank2(sh As Worksheet)
Set LastCell = sh.Cells.SpecialCells(xlCellTypeLastCell)
If LastCell.Value = "" And LastCell.Address = Range("$A$1").Address Then IsBlank2 = True
End Function