+ Reply to Thread
Results 1 to 6 of 6

Count the number of cells in a range that are not empty

  1. #1
    Registered User
    Join Date
    01-01-2005
    MS-Off Ver
    2007
    Posts
    368

    Count the number of cells in a range that are not empty

    Hi,

    Supose I have in column A the folhowing values:
    A1 = 1
    A2 = 2
    A3 = 3

    I also have the folowing function in a module:
    Please Login or Register  to view this content.
    I want to "count the number of cells in a range that are not empty".
    I know that I can get result I want using "=counta(A1:A3)", but I want to use the function above.

    It's possible?

    Regards,
    Elio Fernandes.
    Last edited by efernandes67; 07-23-2010 at 04:40 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count the number of cells in a range that are not empty

    Why not simply use

    =COUNTA(A:A)

    There is little point using UDF in place of native functions given in most cases the UDF will be slower.

    On an aside - passing col as a Byte is not ideal - better to use Long given 2007+ capacity (merit of Integer type in VBA is debatable)

  3. #3
    Registered User
    Join Date
    01-01-2005
    MS-Off Ver
    2007
    Posts
    368

    Re: Count the number of cells in a range that are not empty

    I always knew that I could get the result with COUNTA function by itself.
    I was just trying to know if it was possible to mix the "COUNTA" function with my UDF.
    Thanks for the alert of "Byte".

    Regards,
    Elio Fernandes

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Count the number of cells in a range that are not empty

    If you want to then try
    Please Login or Register  to view this content.
    Entered as
    =FindLastRow("A")

    or
    Please Login or Register  to view this content.
    Entered as
    =CountUsedCells(A:A)

    Hope this helps
    Last edited by Marcol; 07-23-2010 at 05:31 AM.

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Count the number of cells in a range that are not empty

    And FWIW you'd be far better off passing the Column as a Range to the UDF as opposed to a number thereby negating the need for Application.Volatile
    (ie make precedent range explicit as opposed to implicit)

    Given UDF's are generally slow(er) making them Volatile has obvious drawbacks.

    If you're keen on UDFs it would IMO be a good idea to have a read through of Charles Williams' page on the topic if you've not already:

    http://www.decisionmodels.com/calcsecretsj.htm

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Count the number of cells in a range that are not empty

    Totally agreed Don. (Posts #2 &5)

    Just tried to show it was possible, adapting the code given.
    Forgot to take "Application.Volatile" out of the second example.

    My apologies.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1