Counting non-null cells in a column

BobSullivan
3StarLounger
Posts: 235
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Counting non-null cells in a column

Post by BobSullivan »

Hello,

I've got a simple macro in which I'm trying to count the values in a column that are less than one. If I select only specific cells with this macro, it works. If I select the entire column, the empty cells are added to the values less than one. I would like to select the entire column and only count the values that are less than I've tried to write it as a single if statement and as a nested if statement, neither one seems to work. Here's my code:

Code: Select all

Sub countRates()
mycount = 0
totalcount = 0
For Each cellvalue In Selection
    totalcount = totalcount + 1
    If IsNull(cellvalue) = True Then
     nullcells = nullcells + 1
    ElseIf cellvalue < 1 Then
        mycount = mycount + 1
    End If
Next
MsgBox "total exchange rates less than 1 is " & _
mycount & " out of " & totalcount
End Sub
thanks for any help.
Cordially,

Bob Sullivan
Elverson, PA

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

Re: Counting non-null cells in a column

Post by HansV »

An empty cell is not Null. You can use either

Code: Select all

        If IsEmpty(cellvalue) Then
or

Code: Select all

        If cellvalue = "" Then
Best wishes,
Hans

BobSullivan
3StarLounger
Posts: 235
Joined: 08 Jun 2010, 20:03
Location: Morgantown, PA

Re: Counting non-null cells in a column

Post by BobSullivan »

Thank you!
Cordially,

Bob Sullivan
Elverson, PA