+ Reply to Thread
Results 1 to 13 of 13

Counting integers in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Counting integers in VBA

    How do I count the number of integers in a range that includes blank strings and truly blank cells?
    Thanks?

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Counting integers in VBA

    Hi Alan -
    1) Just integers, or any number?
    2) How do you want to handle numbers entered as TEXT?

    Perhaps post some sample data in a workbook...
    Last edited by leelnich; 01-28-2018 at 03:02 PM.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  3. #3
    Forum Contributor
    Join Date
    07-23-2016
    Location
    Texas
    MS-Off Ver
    2016
    Posts
    274

    Re: Counting integers in VBA

    You question is not clear. Do you mean that the cells in the range can have integers, text, and decimals? Do you want to just count the number of cells that have numbers that equate to an integer.

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Counting integers in VBA

    If done in Excel, this would count all numbers NOT entered as text:
    C = Application.Count(Range("A2:G101"))
    Last edited by leelnich; 01-28-2018 at 03:13 PM.

  5. #5
    Forum Guru AlKey's Avatar
    Join Date
    07-20-2009
    Location
    Lakeland, FL USA
    MS-Off Ver
    Microsoft Office 2010/ Office 365
    Posts
    8,903

    Re: Counting integers in VBA

    If you want to use formula try this
    Enter array formula in B2

    ***Array formula
    ...confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER.
    You will know the array is active when you see curly braces { } appear around your formula.
    If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer.

    Formula: copy to clipboard
    =COUNT(IF((MOD(A$2:A$15,1)=0)*(A$2:A$15<>""),A$2:A$15))

    v A B
    1 Count Integers
    2 43 7
    3 88
    4 143
    5 25.0842463
    6 126.3725726
    7 text
    8 87
    9 71.39662062
    10 TEXT
    11 94
    12 137
    13
    14 123.3944593
    15 62
    16
    If you like my answer please click on * Add Reputation
    Don't forget to mark threads as "Solved" if your problem has been resolved

    "Nothing is so firmly believed as what we least know."
    --Michel de Montaigne

  6. #6
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Counting integers in VBA

    to leelnich Reply #4--I know; I just want integers. And the specific problem I'm dealing with is that

       ElseIf Feature = "Integers" Then
                  For i = 1 To NR: For j = 1 To NC
                    If IsNumeric(InRngArray(i, j)) Then
                      If Application.RoundDown(InRngArray(i, j), 0) = InRngArray(i, j) Then
                        tempCount = tempCount + 1
                      End If
                    End If
                  Next: Next
                End If
    treats a truly blank cell as 0, an integer.

  7. #7
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Counting integers in VBA

    I have a function that counts the Size (i.e., rows X columns), Numbers, Blanks or Non-Blanks in an array
    or range depending on which feature is input, and I want to expand it to count integers, currency, and
    probably more features. This the first hurdle.

  8. #8
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Counting integers in VBA

    to leelnich--as text, not a number.

  9. #9
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Counting integers in VBA

    The Excel ISNUMBER function ignores blanks. Try this:
    '...
    ElseIf Feature = "Integers" Then
        For i = 1 To NR: For j = 1 To NC
            If Application.IsNumber(InRngArray(i, j)) Then
                If InRngArray(i, j) = Int(InRngArray(i, j)) Then
                  tempCount = tempCount + 1
                End If
            End If
        Next: Next
    End If
    '...
    Last edited by leelnich; 01-28-2018 at 05:19 PM.

  10. #10
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Counting integers in VBA

    to leelnich--Thanks, perfect! Why the Excel function IsNumber ignores both Blank strings and true Blanks while the VBA Function IsNumeric
    ignores only Blank strings baffles me, but I'll take it.

    Thanks again.

  11. #11
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Counting integers in VBA

    Quote Originally Posted by Alan Beban View Post
    ...Why the Excel function IsNumber ignores both Blank strings and true Blanks while the VBA Function IsNumeric ignores only Blank strings baffles me...
    Amen, brother. Thank you for the rep. - Lee

  12. #12
    Registered User
    Join Date
    08-27-2017
    Location
    Modesto, CA
    MS-Off Ver
    Microsoft Office Excel 2003
    Posts
    91

    Re: Counting integers in VBA

    Although, after having made that comment it occurred to me that once you know about the features of the two functions, and since they are both available
    in VBA, it's really more versatile to have two tools instead of one.

  13. #13
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Counting integers in VBA

    Quote Originally Posted by AlKey View Post
    If you want to use formula try this...
    @AlKey, you could simplify that to:
    =COUNT(IF((MOD(A$2:A$15,1)=0)*(A$2:A$15<>""),1))
    ...and here's another array formula that uses COUNT's ability to ignore #DIV/0! errors:
    =COUNT(1/(MOD(A$2:A$15,A$2:A$15<>"")=0))
    Last edited by leelnich; 01-29-2018 at 01:44 AM.

+ 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. [SOLVED] Need help counting single integers within a text string
    By ksrimmer in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 10-14-2014, 05:21 AM
  2. counting the number of the integers within text?
    By dwx in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-23-2013, 11:45 AM
  3. Counting duplicate integers in cell
    By cowtowner in forum Excel General
    Replies: 1
    Last Post: 05-12-2010, 12:08 PM
  4. A list of Consecutive Integers, can I search for missing integers
    By CM in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 09:05 PM
  5. [SOLVED] A list of Consecutive Integers, can I search for missing integers
    By Harlan Grove in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 09-06-2005, 09:05 AM
  6. A list of Consecutive Integers, can I search for missing integers
    By CM in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 06:05 AM
  7. [SOLVED] A list of Consecutive Integers, can I search for missing integers
    By CM in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM

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