+ Reply to Thread
Results 1 to 16 of 16

Counting values in single cell - Macro

  1. #1
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Post Counting values in single cell - Macro

    Hello all - I am pretty new to VBA programming (just started 2 days ago). I was wondering if what I am trying to do is possible, I searched around online quite a bit before posting. Here's what I want to achieve. I am trying to do this is excel formulas first, and then create a macro out of it.

    I have a big set of data with two columns of items that I would like to count. For example, my data that I want to count is in cell A1 and A2.

    Say I have this:
    Cell A1:::: L9, L10, L56, L23
    Cell B1:::: L99, L41, L5

    I want to count the number of "L?" that I have -- So for the example above, the count should be "7".

    Or if I have the below data, the count should be "5".
    Cell A1:::: L39, L15, L26, L43
    Cell B1:::: L94

    Or if I have the below data, the count should be "1".
    Cell A1:::: L4
    Cell B1:::: (blank)

    I tried using the formula below by looking at values separated by commas, but as you can see...some of the data (particularly the last value) in a cell is not separated by a comma, so this formula did not work. Also if I have a single data point, this formula does not work.

    =LEN(TRIM(H7))-LEN(SUBSTITUTE(TRIM(A1),",",""))+LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1),",","")) ....and similarly for A2.

    I tried doing a "COUNTIF(A1,"L*"), but this does not count repetitive values.


    Any tips that can help me out with this? I would like to create a macro with a click button that achieves this function.

    Thanks in advance!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Counting values in single cell - Macro

    Hello avon9321,

    If you started VBA 2 days ago then you should probably stick with a formula to solve this.

    If the values always start with an "L" then you could use a formula to search for just the number of commas and add 1 if it is greater than 0.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,726

    Re: Counting values in single cell - Macro

    To find the number of L's in A1, you can use this:

    =LEN(A1)-LEN(SUBSTITUTE(A1,"L",""))

    You seem to be making it very complicated.

    Hope this helps.

    Pete

  4. #4
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Re: Counting values in single cell - Macro

    Thank you for your quick response!

    I have a data table of over 400 lines and the values do not always start with "L", it could be any "XX" or "XXX"

    I have created everything else that I want to achieve in this macro with a click button, this is the last part that is giving me some trouble.

  5. #5
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Re: Counting values in single cell - Macro

    Pete - That makes so much more sense, that actually works in the formula!

    What if the values do not always start with "L" ?

  6. #6
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,726

    Re: Counting values in single cell - Macro

    Try it - it should make no difference, as any L within A1 is removed by the second term, wherever it occurs within A1.

    Hope this helps.

    Pete

  7. #7
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Re: Counting values in single cell - Macro

    Your formula does work, but let's say I have this scenario and the formula is in column C (to count)

    Cell A1:::: L9, L10, L56, L23
    Cell B1:::: L99, L41, L5
    = Cell C1: 7


    Cell A2:::: R9, R10, R56, R23
    Cell B2:::: R99,
    = Cell C2: 0
    (I can change C2 to search for "R" instead of "L"), but this would be tedious for all cells if they all have different starting letters)

    Cell A3:::: C9, C10, C56, C23
    Cell B3::::
    = Cell C3: 4
    (I can change C3 to search for "C" instead of "R"), but this would be tedious for all cells if they all have different starting letters)

    Thanks so much for your help!!!

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Counting values in single cell - Macro

    Petes formula would work in all situations if you counted the commas instead of the L.

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  9. #9
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,726

    Re: Counting values in single cell - Macro

    Instead of explicitly using "L" within the formula that I gave you earlier, you can change this to a cell reference, where that cell contains the letter that you want to look for, or if you always want to look for the first letter in the string you can change the formula to this in C1:

    =LEN(A1)-LEN(SUBSTITUTE(A1,LEFT(TRIM(A1)),""))

    and then copy down.

    Hope this helps.

    Pete

  10. #10
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Re: Counting values in single cell - Macro

    mehmetcik,
    Thanks for your reply!

    I actually tried counting the commas initially...but for instances where there is no comma in cell B1 (when there is only one value), this would not work, for example:

    In the case below, the answer should be 5, but I get 4....because cell B1 does not have a comma and only 1 value.

    Cell A1:::: R9, R10, R56, R23
    Cell B1:::: R99


  11. #11
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,726

    Re: Counting values in single cell - Macro

    If you want to count commas instead, then you can do this:

    =LEN(","&A1)-LEN(SUBSTITUTE(","&A1,",",""))

    Hope this helps.

    Pete

  12. #12
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Re: Counting values in single cell - Macro

    AHH! that works! THANK YOU SO MUCH PETE!!!

    That's perfect!

    Can I directly implement this as a macro now?

  13. #13
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,726

    Re: Counting values in single cell - Macro

    I don't know which one you mean, as I just gave you two approaches in quick succession, but yes you can. You need to double-up all the " in the formula.

    Hope this helps.

    Pete

  14. #14
    Registered User
    Join Date
    01-07-2017
    Location
    London
    MS-Off Ver
    Office 2013
    Posts
    10

    Re: Counting values in single cell - Macro

    I used this one:

    =LEN(A1)-LEN(SUBSTITUTE(A1,LEFT(TRIM(A1)),""))

    What do you mean double up the " ?

  15. #15
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,726

    Re: Counting values in single cell - Macro

    Turn the macro recorder on, record a macro while you put that formula in a cell, stop recording, then view the result. It will probably have used the .FormulaR1C1 construct, but can you also see what has happened to the "" at the end of the formula?

    Pete

  16. #16
    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 values in single cell - Macro

    Try this
    Enter in C1 and copy down
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    v A B C D
    1 L9, L10, L56, L2 L99, L41, L5 7 L
    2 R9, R10, R56, R23 R99 5 R
    3 C9, C10, C56, C23 4 C
    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

+ 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] Counting Dates in a Single Cell
    By Rol.S in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 10-09-2016, 07:39 AM
  2. Replies: 6
    Last Post: 04-01-2016, 05:40 PM
  3. [SOLVED] Excel Marco How to put multiple rows text values into single cell using macro
    By spa3212 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2015, 09:49 AM
  4. [SOLVED] Counting multiple values in single cells
    By guykescheep in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-08-2013, 01:39 PM
  5. [SOLVED] Macro to replace every single cell in each worksheet with values only
    By Bishonen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2012, 10:42 AM
  6. Counting multiple values in a single cell
    By ac8038 in forum Excel General
    Replies: 8
    Last Post: 06-20-2006, 09:30 AM
  7. [SOLVED] Counting values and comparing them to a single cell
    By Tbentsen in forum Excel General
    Replies: 3
    Last Post: 08-04-2005, 06:05 PM

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