+ Reply to Thread
Results 1 to 9 of 9

Count function

Hybrid View

BryceVBA Count function 03-23-2017, 10:19 AM
YasserKhalil Re: Count function 03-23-2017, 10:42 AM
BryceVBA Re: Count function 03-23-2017, 11:19 AM
gondal Re: Count function 03-23-2017, 10:47 AM
BryceVBA Re: Count function 03-23-2017, 11:22 AM
gondal Re: Count function 03-23-2017, 11:23 AM
BryceVBA Re: Count function 03-23-2017, 11:37 AM
MarvinP Re: Count function 03-23-2017, 11:26 AM
Phuocam Re: Count function 03-23-2017, 11:43 AM
  1. #1
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Count function

    Hello, I need to count how many cells have entries in column A. I have the following code so far, but the variable is returning 17 instead of the 2 entries that actually exist. Does anyone know a more reliable count function that wont include blanks?

    Dim CountVar As Long
        CountVar = Application.CountA(Columns("A"))

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Count function

    May be you have some cells with space in them " " so try this
    Sub Test()
        Dim countVar As Long
        countVar = Application.CountA(Columns(1)) - Application.CountIf(Columns(1), " ")
        MsgBox countVar
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Count function

    Quote Originally Posted by YasserKhalil View Post
    May be you have some cells with space in them " " so try this
    Sub Test()
        Dim countVar As Long
        countVar = Application.CountA(Columns(1)) - Application.CountIf(Columns(1), " ")
        MsgBox countVar
    End Sub

    This code gave me 500 returns instead of 17. haha

  4. #4
    Forum Contributor
    Join Date
    04-19-2015
    Location
    pakistan
    MS-Off Ver
    2013
    Posts
    335

    Re: Count function

    try this
    Sub Test()
        Dim lngCount As Long
    lngCount = Application.WorksheetFunction.CountA(Columns(1))
    MsgBox lngCount
    End Sub

  5. #5
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Count function

    Quote Originally Posted by gondal View Post
    try this
    Sub Test()
        Dim lngCount As Long
    lngCount = Application.WorksheetFunction.CountA(Columns(1))
    MsgBox lngCount
    End Sub
    Same with this code. 500 returns

  6. #6
    Forum Contributor
    Join Date
    04-19-2015
    Location
    pakistan
    MS-Off Ver
    2013
    Posts
    335

    Re: Count function

    can you upload the sheet..??

  7. #7
    Forum Contributor
    Join Date
    02-20-2017
    Location
    Indiana
    MS-Off Ver
    2016
    Posts
    101

    Re: Count function

    So instead of looking in the whole column, I limited the search to A1:A10, since this variable shouldn't have too many entries.

    CountVar = WorksheetFunction.CountA(Range("A1:A10"))
    Also I owe you both an apology. I was originally using your code with a report that the code isn't compatible with yet, so the results were invalid. I tried both of your codes again with compatible reports and they both returned 17 still.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,252

    Re: Count function

    Hi Bryce,

    You could also have a formula in a cell that returns a blank. This would look like a blank cell but be counted in the CountA function.

    https://www.thoughtco.com/counting-a...f-data-3123571
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  9. #9
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2024
    Posts
    6,175

    Re: Count function

     
    countVar = Application.CountIf(Columns("A"), "?*")

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 17
    Last Post: 04-11-2016, 11:14 PM
  2. User defined function- count color and count only visible rows
    By marsjanik1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-17-2016, 03:05 PM
  3. How to count text entered with ALT+ENTER using a count function
    By bethy1234 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-29-2015, 02:41 PM
  4. [SOLVED] COUNT function does not count values beginning with text
    By PeteABC123 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-15-2013, 06:38 PM
  5. [SOLVED] Count If Function To Count Frequency Of Long Numbers
    By RowanB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2013, 05:18 AM
  6. Using the COUNT function to count letters generated by an IF function
    By SMErickson7 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-28-2013, 12:02 PM
  7. Replies: 2
    Last Post: 06-23-2009, 11:16 AM

Tags for this Thread

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