+ Reply to Thread
Results 1 to 13 of 13

Counting the number of occurances in a single row

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Counting the number of occurances in a single row

    hi,

    I am writing a vba program in excel and i have come across a situation where i need some help.

    I have a row of column headers. This row has lots of information in it. There are "quizes" that have column labels of E1, E2, E3....En. What i want to do is somehow count the number of quizes (or E#'s) that may be in this row. Anyone have any ideas on how to do this.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Counting the number of occurances in a single row

    Hi,

    Do you really need a macro to do this? Why not use the Excel COUNTIF function. If necessary you could then pick this value up in your VBA program.

    Assuming your column headers are in say E10:Z10 in another cell use

    Please Login or Register  to view this content.
    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: Counting the number of occurances in a single row

    Yes, i need a macro to do this.........i am writing a program and this is something ineed to do within the program.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Counting the number of occurances in a single row

    Hi,

    Well in that case the simplest way would be to have the macro use that same formula.

    i.e.
    Please Login or Register  to view this content.
    HTH

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: Counting the number of occurances in a single row

    thanks...this almost works.

    The problem is i only want to count entries like the following:
    E1, E2, E3, ......En


    I dont want to count things like
    E1-1, E2-1.....etc

  6. #6
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Counting the number of occurences in a single row

    Hi,

    Building on Richard's code, with your new requirement :
    Please Login or Register  to view this content.
    HTH

  7. #7
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Counting the number of occurances in a single row

    Or using Richards' code

    Please Login or Register  to view this content.
    Change "=E*" to "=E?"

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Counting the number of occurances in a single row

    A criteria of "E?" will only count those instances where E is followed by a.n.other character and where string has 2 chars in total.

    EDIT: Marcol beat me to it

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: Counting the number of occurances in a single row

    thanks! i think i can make one of these work.

  10. #10
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: Counting the number of occurances in a single row

    i just noticed that if i have E10 or above like E11, E12, E13........this method does not count those. Any suggestions?

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,679

    Re: Counting the number of occurances in a single row

    i modified the code to handle up to 3 characters.....but i was wondering if there was a better way

    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Re: Counting the number of occurances in a single row

    Hi,

    From what you said so far, you need a pattern based on all your potential cases...
    Have a try with : ="E*?"

    HTH

  13. #13
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Counting the number of occurances in a single row

    I'm not sure a criteria of E*? would work given E11-1 for ex. would be seen as valid (we know this not to be the case).

    It would help if all variations were outlined.

    One basic alternative based on requirements outlined thus far might be to use:

    COUNTIF(range,"E*")-COUNTIF(range,"E*-*")

    but if there are varations other than hyphen to be excluded then the above would be open to fail also.

    More info. required I think.

+ 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