+ Reply to Thread
Results 1 to 13 of 13

Count occurrences of text between blanks

  1. #1
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Count occurrences of text between blanks

    In my spreadsheet the data looks like this:

    Column G
    [blank]
    24
    8
    12
    [blank]
    1
    23
    10
    12
    [blank]
    1
    18
    23
    25
    8
    [blank]
    15
    18
    11

    I am trying to find a way to count the occurrence of the numbers (stored as text) in each grouping - i.e. between the blanks - and return the value on each line within the grouping.
    So for example, it would look like this:

    Column G,Column H
    24,3
    8,3
    12,3
    ,0
    1,4
    23,4
    10,4
    12,4
    ,0
    1,5
    18,5
    23,5
    25,5
    8,5
    ,0
    15,3
    18,3
    11,3

    I have wracked my brain to use nested if statements to try and count between the blanks, to no avail. I believe this would be most easily done with a macro. Any help would be greatly appreciated!

    Thanks,
    Tannenbaum

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Count occurrences of text between blanks

    Here's a quick macro which should give what you want:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Re: Count occurrences of text between blanks

    Thanks so much for the response! This is for an important project so I really appreciate it.

    I got the code to work as expected, however I had to change things up a bit in my sheet to get it to work. I can live with that work around if I have to, but it would be much better for the client without having to do so.

    The problem is, I have lots of data to the left of my example data above in the spreadsheet - specifically, in column A there is data on every line that aligns with the blanks in column G. The macro is reading that text and counting it with the data in column G, then in column H (in the cells that correspond (as expected) to text in column G) returning the total of all values (happens to be 190).

    When I run the macro in a sheet with nothing in it but the column G data, it works great. However, it would be really nice to be able to run it in the main sheet, and have it ignore data in other columns, if possible. Also it replaces my column header in column H with the number 1 (or 190 in the case of the main sheet), corresponding to the header in column G.

    If you can make it adjust to these conditions (ignore data in adjacent columns that align with the blanks in G, and don't replace the header in the output column (H)) I'd appreciate it.

    Thanks again,

    Tannenbaum

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Count occurrences of text between blanks

    Try this:
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Count occurrences of text between blanks

    FWIW:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Re: Count occurrences of text between blanks

    Thanks Olly, that solved the header problem - it no longer replaces the header.

    However now the output looks like this:

    Column5,Column6

    10 (B...,3
    11 (R...,190
    13 (S...,190

    1 (A...,4
    5 (E...,190
    13 (S...,190
    14 (S...,190

    The 3s and 4s are all correct, but it fills the rest in with total counts like before. Tricky problem!

    Tannenbaum
    ---
    Thanks John, I tried that, and there was an error with line 2 - "For Each..." does it have to do with "Columns(7)"? I tried a number of different substitutions (like "Columns(G:G)", "Columns(,7)", and a few others) and couldn't get it to run.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Count occurrences of text between blanks

    Can you attach a sample of your workbook to test on. Preferrably one which produces the error. Read to Forum Rules for guidance on attachments.

    http://www.excelforum.com/forum-rule...rum-rules.html

  8. #8
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Re: Count occurrences of text between blanks

    Tannenbaum1, Count text btw blanks spreadsheet.xlsm

    Here is an attachment with my data (semi-redacted) and the most recent macros from Olly and John.

    Olly, I figured out, the problem I was experiencing was due to data existing also to the right of the output column (H). In my instructions I can simply say to run the macro before those columns are created, which mostly solves the problem as you can see in the attachment. However, scroll down to the bottom and notice it got the last three numbers wrong for some reason and went outside of the table.

    Thanks again all for your help.

    Tannenbaum.
    Last edited by Tannenbaum1; 12-16-2014 at 11:47 AM.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Count occurrences of text between blanks

    My suggestion was set up for numerical data vs. alphanumeric.

  10. #10
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Re: Count occurrences of text between blanks

    John, your's works great if I filter column G on values, fill H with 1s (corresponding to lines with text in G), un-filter G, make the macro point to the column with 1s, then run it. It outputs the correct values in I.

    I can work with that. However Olly (or John) - if you can resolve the last issue with the macro from #4 above (the miscalculation at the end of the output), that macro would save the extra steps. Otherwise problem solved.

    Thanks!

    Tannenbaum.
    Last edited by Tannenbaum1; 12-16-2014 at 12:18 PM.

  11. #11
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Re: Count occurrences of text between blanks

    Nm - see #10.
    Last edited by Tannenbaum1; 12-16-2014 at 12:18 PM.

  12. #12
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Count occurrences of text between blanks

    In your sample file, cell G190 has a nonprintable character (check by entering =ISBLANK(G190) in an empty cell.

    Fix by deleting this cell, then run the macro.

    Here's a tweak which allows it to work with other data to left and/or right of the target column:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    11-10-2014
    Location
    Washington, DC
    MS-Off Ver
    2010
    Posts
    10

    Re: Count occurrences of text between blanks

    Thanks Olly, that works great!

+ 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. count the occurrences of text
    By Elainefish in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-01-2013, 09:32 AM
  2. [SOLVED] Count occurrences of text in a range
    By braydon16 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 03-18-2013, 08:33 PM
  3. Replies: 3
    Last Post: 01-09-2013, 07:55 PM
  4. Count occurrences of text with various criteria
    By ludgirardi in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 12-20-2012, 05:31 AM
  5. Count occurrences of text with various criteria
    By ludgirardi in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 12-18-2012, 07:50 PM

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