PC Review


Reply
Thread Tools Rate Thread

Determine if Column Populated

 
 
kirkm
Guest
Posts: n/a
 
      9th Apr 2008

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
 
Reply With Quote
 
 
 
 
Tom Hutchins
Guest
Posts: n/a
 
      9th Apr 2008
This is pretty easy...

Public Function IsColEmpty(WhichCol As String) As Integer
IsColEmpty = Application.WorksheetFunction.CountA(Columns(WhichCol))
End Function

You would call it like this...

Sub AAAA()
MsgBox IsColEmpty("L")
End Sub

Hope this helps,

Hutch

"kirkm" wrote:

>
> 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
>

 
Reply With Quote
 
Leith Ross
Guest
Posts: n/a
 
      9th Apr 2008
On Apr 8, 6:56 pm, kirkm <x...@xx.com> wrote:
> 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


Hello Kirk,

Here is a simple way in VBA. Just change the column letter to match
the range to check.

N = WorksheetFunction.CountA(Range("D"))

Sincerely,
Leith Ross
 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      9th Apr 2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
kirkm
Guest
Posts: n/a
 
      9th Apr 2008
Wow three replies!

Thank you all very much - interesting to see the variations.
I ended up applying Ricks as the last column filled could be useful.

Much appreciated... you chaps are great

Cheers - Kirk
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Last populated cell in a column RedFive Microsoft Excel Misc 9 24th Aug 2010 07:19 PM
Determine which cell was populated Jock Microsoft Excel Programming 3 5th Oct 2009 05:33 PM
Last populated cell in a column RedFive Microsoft Excel Misc 1 3rd Oct 2008 09:01 PM
Copying a value till the last populated column EE Microsoft Excel Programming 3 17th Aug 2007 12:45 AM
How to determine when a DataGridView's DataSource is finished being populated johneevo Microsoft ADO .NET 2 5th Apr 2006 06:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:49 PM.