Results 1 to 8 of 8

Thread: Excel VBA - CountIF

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Excel VBA - CountIF

    Hey,

    I'm new to VBA and have hit a roadblock in what im trying to do. I have a column of data containing numbers from 10-360 in intervals of 10. All I need to do is calculate the percentage of each value in the column. For example, I need to count the number of entries of "10" and divide by the total number of entries. I did this using =COUNTIF(A2:A2200,"=10.0")/(2199) and so on but it was very time-intensive. I am looking for an easier way to use COUNTIF in VBA. If anyone can help get me started it would be appreciated. Thanks.

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Excel VBA - CountIF

    Try this

    you need to create a new sub and paste the following code:

    vb Code:
    1. 'this is for the loop variable
    2.     Dim i As Integer
    3.    
    4.     'this will be the target range
    5.     Dim r As Range
    6.    
    7.     'this will be the target row
    8.     Dim rw As String
    9.    
    10.     'this is the beginning constant portion of the formula
    11.     Dim str1 As String
    12.    
    13.     'this is the ending portion of the formula that changes
    14.     Dim str2 As String
    15.    
    16.    
    17.     'this makes the target start at row 2
    18.     'change this as necessary for the row that you want it to start on
    19.     rw = 2
    20.    
    21.     'this is just the beginning portion of your formula
    22.     str1 = "=Countif(A2:A30,"
    23.    
    24.  
    25.     'this for loop sets i to start at the value of 10, changes by +10 (Step 10) for each iteration until it = 360
    26.     For i = 10 To 360 Step 10
    27.         'this sets the target cell to be "C" column at the row that the rw is set to
    28.         Set r = Range("C" & rw)
    29.         'this sets the variable equal to the first portion of your formula
    30.         'the & i adds the value of i, which starts at 10 and goes up in increments of 10
    31.         str2 = str1 & i & ")/(2199)"
    32.         'this puts the formula in the target cell
    33.         r.Formula = str2
    34.         'this advances the target to the next row
    35.         rw = rw + 1
    36.     Next i

    If you need more help, or you want to alter this to go across the columns instead of down rows, let me know and I'll update the code.
    Last edited by nO_OnE; Aug 10th, 2011 at 08:01 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Re: Excel VBA - CountIF

    hey thanks it worked great! The only other thing i am looking into doing is the denominator in the formula str2 =.../(2199) is actually equal to the total number of entries in column A2:A30(for example). Is it possible to change the denominator from 2199 to the count of non-blank data entries in a particular column? Thanks!

  4. #4
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Excel VBA - CountIF

    You can, when you are talking about blank cells, do you mean that there may be random blank cells, or would it work if we determined the last cell with data in it?

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Excel VBA - CountIF

    If I understand it right, it is just a matter of writing the right formula, no need for code. Lets say you have your numbers in column C, put the list on column D (10,20,30,40...360) put this formula in E4 and just copy it to the rest of the column.

    =COUNTIF($C$4:$C$2200,"=" & D4)/COUNT($C$4:$C$2200)

    The only flaw here is that it is only checking until row 2200, of course you can just put a huge number there in hopes that it is always covering your data. But I would prefer to create a named range and use it instead of the absolute references

    =COUNTIF(myRange,"=" & D4)/COUNT(myRange)
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  6. #6
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Excel VBA - CountIF

    I was just looking that up, I forgot about counting the cells with data. I am used to just using the end function on Range.

    So, Try:
    Code:
        Dim i As Integer
        Dim r As Range
        Dim rw As String
        Dim str1 As String
        Dim str2 As String
        
        rw = 2
        str1 = "=Countif(A2:A30,"
        
        For i = 10 To 360 Step 10
            Set r = Range("C" & rw)
            str2 = str1 & i & ")/(Count(A2:A2200))"   'Replaced the original 2199 with the count function
            r.Formula = str2
            rw = rw + 1
        Next i

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    15

    Re: Excel VBA - CountIF

    Hi,

    Thanks for the help. One last question regarding this issue. I now have one column of data that differentiates between > or < Wind speed by saying either "LS 15" or "GT 15". I need to use the CountIf macro above to perform 2 calculations, one for data with LS 15 and one for data with GT 15. I was wondering the best way to incorporate this into the macro, or if I need to do it another way. Thanks for the help.

  8. #8
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Excel VBA - CountIF

    If those are the only two items you need to differentiate from then you can use a simple if statement. I am assuming that you need to utilize it on the formula above:
    vb Code:
    1. Dim i As Integer
    2.     Dim r As Range
    3.     Dim rw As String
    4.     Dim str1 As String
    5.     Dim str2 As String
    6.    
    7.     rw = 2
    8.     str1 = "=Countif(A2:A30,"
    9.    
    10.     For i = 10 To 360 Step 10
    11.         Set r = Range("C" & rw)
    12.         If Left(r.Value, 2) = "LS" Then
    13.             str2 = str1 & i & ")/(Count(A2:A2200))"
    14.             r.Formula = str2
    15.         ElseIf Left(r.value, 2) = "GT" Then
    16.             'your secondary formula
    17.         End If
    18.         rw = rw + 1
    19.     Next i

    Or you can use the select case method, which is better for differentiating between more items:
    vb Code:
    1. Dim i As Integer
    2.     Dim r As Range
    3.     Dim rw As String
    4.     Dim str1 As String
    5.     Dim str2 As String
    6.    
    7.     rw = 2
    8.     str1 = "=Countif(A2:A30,"
    9.    
    10.     For i = 10 To 360 Step 10
    11.         Set r = Range("C" & rw)
    12.         Select Case Left(r.Value, 2)
    13.             Case "LS"
    14.                 str2 = str1 & i & ")/(Count(A2:A2200))"
    15.                 r.Formula = str2
    16.             Case "GT"
    17.                 'Your code for GT
    18.             End Select
    19.         rw = rw + 1
    20.     Next i

Posting Permissions

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



Click Here to Expand Forum to Full Width