Results 1 to 22 of 22

Thread: [RESOLVED] Excel VBA - Count Occurances In Column

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Resolved [RESOLVED] Excel VBA - Count Occurances In Column

    I have a column on a spreadsheet with a Title of "Action"

    In this column, there will either be the word "Add" "Delete" or "Update"

    As of the moment, the column goes from G:3 to G:731, but that will grow down.

    I need to skip down two from the bottom (I know how to do this) and Count howmany times the word "Add", how many times the word "Delete", how many times the word "Update" appear as well as how many times there is nothing at all listed in this column.

    I spent some time Googling =COUNT but I can't find any examples that satisify what I need to do.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    Hi Hack,

    i could do this with a macro, if that's any help ??

    not sure of an actual excel function to do this.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    Quote Originally Posted by NeedSomeAnswers
    i could do this with a macro, if that's any help ??
    Sure...that works!

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    Quick question,

    when you say -
    I need to skip down two from the bottom
    does that mean if you range is from G:3 to G:731 you only want to count until G729 ?

    and secondly where do you want to put the totals ?

    i could just messagebox them in the example, and let you sort that bit if you want ?
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    Just messagebox them. I'll figure out where they should go.

  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    This should do it,

    you need to provide it with the last cell of the range. I cant remember how to make it figure this out for you when there are blanks in the column off the top of my head, i think i have some code at home though if you really need it ? Let me know !

    vb Code:
    1. Sub GetAction()
    2.  
    3. Dim RANGEBOTTOM As String
    4. Dim cell
    5. Dim strAction As String
    6. Dim intAdd As Integer
    7. Dim intUpdate As Integer
    8. Dim intDelete As Integer
    9. Dim intBlank As Integer
    10.  
    11. RANGEBOTTOM = "G731"
    12.  
    13. For Each cell In Range("G3:" & RANGEBOTTOM)
    14.  
    15.     strAction = cell.Value
    16.    
    17.     If UCase(strAction) = "ADD" Then
    18.         intAdd = intAdd + 1
    19.     ElseIf UCase(strAction) = "UPDATE" Then
    20.         intUpdate = intUpdate + 1
    21.     ElseIf UCase(strAction) = "DELETE" Then
    22.         intDelete = intDelete + 1
    23.     ElseIf strAction = "" Then
    24.         intBlank = intBlank + 1
    25.     End If
    26.    
    27. Next
    28.  
    29. MsgBox "Additions =" & CStr(intAdd)
    30. MsgBox "Updates =" & CStr(intUpdate)
    31. MsgBox "Deletions =" & CStr(intDelete)
    32. MsgBox "blanks =" & CStr(intBlank)
    33.  
    34. End Sub
    Last edited by NeedSomeAnswers; Sep 17th, 2008 at 10:38 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  7. #7

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    Thank you sir! I'll give it a shot.

  8. #8
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    I have made a minor amendment which you might find useful, in this version it should count down to the bottom of your range for you ignoring blanks until it can find no more data, so in effect getting the bottom of your range for you.

    edit - messed up the formatting in this post so posted it again below !!
    Last edited by NeedSomeAnswers; Sep 17th, 2008 at 11:07 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  9. #9
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    vb Code:
    1. Sub GetAction()
    2.  
    3. Dim blnBottom As Boolean
    4. Dim RANGEBOTTOM As String
    5. Dim ACTUALRANGEBOTTOM As String
    6. Dim cell
    7. Dim strAction As String
    8. Dim intAdd As Integer
    9. Dim intUpdate As Integer
    10. Dim intDelete As Integer
    11. Dim intBlank As Integer
    12.  
    13. Dim strrange As String
    14. Dim n As Integer
    15.  
    16. startrange = "G3"
    17.  
    18. n = 0
    19.  
    20. blnBottom = False
    21.  
    22. strrange = CStr(startrange) & ":" & "G5000"
    23.  
    24. Do Until blnBottom = True
    25.  
    26. ACTUALRANGEBOTTOM = RANGEBOTTOM
    27.  
    28. If n <> 0 Then
    29. startrange = "G" & RANGEBOTTOM
    30. strrange = CStr(startrange) & ":" & "G5000"
    31. End If
    32.  
    33. RANGEBOTTOM = Sheet1.range(strrange).End(xlDown).Row
    34.  
    35. If RANGEBOTTOM = "1048576" Then
    36. blnBottom = True
    37.  
    38. End If
    39.  
    40. n = n + 1
    41.  
    42. Loop
    43.  
    44. 'RANGEBOTTOM = "G731"
    45.  
    46. For Each cell In range("G3:" & ACTUALRANGEBOTTOM)
    47.  
    48. strAction = cell.Value
    49.  
    50. If UCase(strAction) = "ADD" Then
    51. intAdd = intAdd + 1
    52. ElseIf UCase(strAction) = "UPDATE" Then
    53. intUpdate = intUpdate + 1
    54. ElseIf UCase(strAction) = "DELETE" Then
    55. intDelete = intDelete + 1
    56. ElseIf strAction = "" Then
    57. intBlank = intBlank + 1
    58. End If
    59.  
    60. Next
    61.  
    62. MsgBox "Additions =" & CStr(intAdd)
    63. MsgBox "Updates =" & CStr(intUpdate)
    64. MsgBox "Deletions =" & CStr(intDelete)
    65. MsgBox "blanks =" & CStr(intBlank)
    66.  
    67. End Sub
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - Count Occurances In Column

    Hi Hack

    Do you need to do it via code as you can achieve the same with a very simple native function called Countif()

    for example

    =COUNTIF(A1:A6,"Add")
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Count Occurances In Column

    @Hack, in case you still want to count in code, this should be quicker:
    Code:
    Sub ActionCount()
       Dim AddCount    As Long
       Dim DeleteCount As Long
       Dim UpdateCount As Long
       Dim BlankCount  As Long
       Dim OthersCount As Long
       Dim ActionRange As Range, r1 As Long, r2 As Long
       
       With Sheet1 '-- your data sheet
          r1 = 3
          r2 = .Cells(.Rows.Count, "G").End(xlUp).Row
          If r2 < r1 Then r2 = r1
          Set ActionRange = .Range("G" & r1 & ":G" & r2)
       End With
       
       With Application.WorksheetFunction
          AddCount = .CountIf(ActionRange, "Add")
          DeleteCount = .CountIf(ActionRange, "Delete")
          UpdateCount = .CountIf(ActionRange, "Update")
          BlankCount = .CountBlank(ActionRange)
       End With
       OthersCount = (r2 - r1 + 1) - (AddCount + DeleteCount + UpdateCount + BlankCount)
       MsgBox "Add" & vbTab & ":  " & AddCount & vbCrLf & _
              "Delete" & vbTab & ":  " & DeleteCount & vbCrLf & _
              "Update" & vbTab & ":  " & UpdateCount & vbCrLf & _
              "Blank" & vbTab & ":  " & BlankCount & vbCrLf & _
              "Others" & vbTab & ":  " & OthersCount & vbCrLf & _
              "––––––––––––––" & vbCrLf & _
              "Total" & vbTab & ":  " & (r2 - r1 + 1), _
              vbInformation, "Action Count"
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    Hi Ahn,

    I particularly like your set ActionRange code -

    Code:
    Dim ActionRange As Range, r1 As Long, r2 As Long
       
       With Sheet1 '-- your data sheet
          r1 = 3
          r2 = .Cells(.Rows.Count, "G").End(xlUp).Row
          If r2 < r1 Then r2 = r1
          Set ActionRange = .Range("G" & r1 & ":G" & r2)
       End With
    Haven't come across this before, and will be using that now in the future

    Cheers !!
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Count Occurances In Column

    Quote Originally Posted by NeedSomeAnswers
    Hi Ahn,

    I particularly like your set ActionRange code -

    Code:
    Dim ActionRange As Range, r1 As Long, r2 As Long
       
       With Sheet1 '-- your data sheet
          r1 = 3
          r2 = .Cells(.Rows.Count, "G").End(xlUp).Row
          If r2 < r1 Then r2 = r1
          Set ActionRange = .Range("G" & r1 & ":G" & r2)
       End With
    Haven't come across this before, and will be using that now in the future

    Cheers !!
    By using Sheet.Rows.Count, it will work for both Excel-2007 and prior. You don't need to know how many rows a worksheet has.

    Using BottomCell.End(xlUp) is better than using TopCell.End(xlDown), particularly when there are some blank cells in the middle.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  14. #14
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Excel VBA - Count Occurances In Column

    Yeh,

    previously I have tried the xlDown method, but obviously as you said it's no good if you have blank cells, I hadn't thought about counting from the bottom upwards !!

    My way was just an ugly work around.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  15. #15

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    While I like coding solutions, native functions work as well.

    Thanks to all.

  16. #16
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Count Occurances In Column

    Is that resolved? Mark it.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  17. #17

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    Quote Originally Posted by anhn
    Is that resolved? Mark it.
    I am aware of the resolution requirements anhn.

  18. #18

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    What is the difference =COUNT(whatever) and =COUNTIF(Whatever)

    I know that in my little spreadsheet, using COUNTIF returns the correct number where COUNT returns 1, but why is that?

    If you need COUNTIF, then what is the purpose of COUNT?

  19. #19
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - Count Occurances In Column

    Hack:

    COUNT() is used get the number of numeric entries.
    Similarly COUNTA() is used get the count of Alphanumeric entries.

    Countif() Counts the number of cells within a range that meet the given criteria.

    For example see the pic
    Attached Images Attached Images  
    Last edited by Siddharth Rout; Sep 18th, 2008 at 07:11 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  20. #20
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - Count Occurances In Column

    @Hack, everything you need to know about different kinds of counting functions is in Excel Help:
    Attached Images Attached Images  
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  21. #21

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - Count Occurances In Column

    Thanks koolsid!

  22. #22
    Registered User
    Join Date
    Jul 2013
    Posts
    1

    Re: Excel VBA - Count Occurances In Column

    Dear all,

    Firstly, this post looks promising wrt the requirement I have.

    Hoping that someone will hear me on this cause I'm reply to a post which is 5 years old.

    Here's my requirement though:

    I have a table called "Names" having the following details:


    No. Name
    1 Anderson
    1 Anderson
    2 Blake
    2 Blake
    2 Blake
    3 Conan


    I would like the Output to be as follows:

    No. Count
    1 2
    2 3
    3 1


    Would love to have a generic VBA code to do this.

    Any help or hint on this would be much appreciated.

    Thanks,
    Vinod Krishna

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