View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_1667_] Rick Rothstein \(MVP - VB\)[_1667_] is offline
external usenet poster
 
Posts: 1
Default Determine if Column Populated

Here is a robust function that will do what you asked plus a little bit
more....

Function LastFilledRow(ColumnNumber As Variant, Optional _
WorksheetID As Variant) As Long
Dim WS As Worksheet
On Error GoTo Whoops
If IsMissing(WorksheetID) Then
Set WS = ActiveSheet
Else
Set WS = Worksheets(WorksheetID)
End If
LastFilledRow = WS.Cells(WS.Rows.Count, ColumnNumber).End(xlUp).Row
If LastFilledRow = 1 And IsEmpty(WS.Cells(1, ColumnNumber)) Then
LastFilledRow = LastFilledRow - 1
End If
Exit Function
Whoops:
LastFilledRow = -1
End Function

The function's arguments allows you to specify the column by its number or
letter designation and, optionally, the worksheet by its caption or sheet
index number. The function returns the last column with anything in it. If
the column has nothing in it, it returns 0 (as per your original request)
and, if an invalid argument is supplied, it returns -1.

Rick



"kirkm" wrote in message
...

In code, what would be the best / easiset way to
determine if a column has anything in any of
its rows (irrespective of how many rows) ?

I'll like to code a function that returns 0 if an entire column
is empty.

Thanks - Kirk