+ Reply to Thread
Results 1 to 8 of 8

Help with using VBA to count number of times different values appear in a column

  1. #1
    Registered User
    Join Date
    10-25-2012
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2003
    Posts
    14

    Help with using VBA to count number of times different values appear in a column

    Hi,

    I am struggling to come up with a solution to my problem. I have a list of, for example, fruit and the names of these fruit appear more than once in a list. I do not know the names of the fruit prior to executing a piece of code so cannot lookup from anywhere. I have produced the following code which works, except it includes the name of the fruit in cell C2 and it's corresponding count twice. Code:

    Please Login or Register  to view this content.
    I have also attached sample data.

    If anyone can help me understand where I am going wrong I would be grateful. Also suggestions on how to improve the code in general is welcome!

    Thanks

    Book1.xls

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

    Re: Help with using VBA to count number of times different values appear in a column

    Hi compto and welcome to the forum,

    I think this is an easy Pivot Table answer. See the attached that does a quick pivot table of your data with counts.
    Attached Files Attached Files
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    10-25-2012
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Help with using VBA to count number of times different values appear in a column

    Thanks for the welcome MarvinP and for the suggestion. A PivotTable was my first idea but unfortunately I need this piece of code for about 15 different worksheets in the same workbook and when I had a pivot table on every sheet the file size ballooned and made it very slow. So I thought a piece of code would be more efficient unless I'm mistaken?

  4. #4
    Forum Contributor
    Join Date
    11-26-2011
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    159

    Re: Help with using VBA to count number of times different values appear in a column

    Quote Originally Posted by compto View Post
    Hi,

    I am struggling to come up with a solution to my problem. I have a list of, for example, fruit and the names of these fruit appear more than once in a list. I do not know the names of the fruit prior to executing a piece of code so cannot lookup from anywhere. I have produced the following code which works, except it includes the name of the fruit in cell C2 and it's corresponding count twice. Code:

    Please Login or Register  to view this content.
    I have also attached sample data.

    If anyone can help me understand where I am going wrong I would be grateful. Also suggestions on how to improve the code in general is welcome!

    Thanks

    Attachment 191457
    Hi,

    Look @ sheet name once whether sheet name has mismatch with code or having spaces... again and try it...

  5. #5
    Registered User
    Join Date
    10-25-2012
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Help with using VBA to count number of times different values appear in a column

    The sheet name is fine and cannot see any problem with spacing either. The code works if the fruit in cell C2 does not appear again and count returns as "1" like it should. The problem only occurs when the fruit in the first cell of the list, C2, appears again further down the list.

  6. #6
    Registered User
    Join Date
    10-25-2012
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Help with using VBA to count number of times different values appear in a column

    I have also come across another problem. When there are blank cells at the top of the list (as there will be on occassion in my full model) the code falls over so it is clearly not robust. Can anyone suggest any tweaks or a better way to achieve what i want? File with the new error attached also. Thanks
    Book2.xls

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

    Re: Help with using VBA to count number of times different values appear in a column

    Hi compto,

    Look at the attached that uses a Dynamic Named Range and will count the number of fruits you have...
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    10-25-2012
    Location
    Cardiff, Wales
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Help with using VBA to count number of times different values appear in a column

    Thanks MarvinP, I have a solution for what I wanted now which doesn't use a DNR but I did learn something new from what you posted so I appreicate your time!

+ 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