Consulting

Results 1 to 8 of 8

Thread: Help to convert COUNTIFS to VBA

  1. #1

    Help to convert COUNTIFS to VBA

    I have used a combination of COUNTIF & COUNTIFS formulas to calculate the age of members from 7 sites
    While it works it is slow to execute and cumbersome to manage.
    Sample code
        Range("D10").FormulaR1C1 = "=COUNTIFS(SITE,""Botany"",AGE,""<=18"")"
        Range("E10").FormulaR1C1 = "=COUNTIFS(SITE,""Botany"",AGE,"">=19"",AGE,""<=20"")"
        Range("F10").FormulaR1C1 = "=COUNTIFS(SITE,""Botany"",AGE,"">=21"",AGE,""<=25"")"
        Range("G10").FormulaR1C1 = "=COUNTIFS(SITE,""Botany"",AGE,"">=26"",AGE,""<=30"")"
        Range("H10").FormulaR1C1 = "=COUNTIFS(SITE,""Botany"",AGE,"">=31"",AGE,""<=35"")"
        Range("I10").FormulaR1C1 = "=COUNTIFS(SITE,""Botany"",AGE,"">=36"",AGE,""<=40"")"
    The named ranges SITE & Botany are full column ranges. My first problem is that the sites may vary from time to time. Is there a way to have the formula look up a list to get the name for each site.

    Is there a way to streamline the formulas in VBA. I'm keen to learn more about VBA

    I have attached an example workbook.

    Cheers
    Attached Files Attached Files
    Last edited by Aussiebear; 03-31-2016 at 07:22 PM. Reason: Added missing bracket to tag

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Not sure you need to use VBA

    I added a little Age Group table to take the age and put it into a helper column

    The COUNTIFS formula can just use the row readers in B7:B19 and the column headers in D9:N9

    One formula with B column fixed and row 6 fixed can be copied to the data block

    =COUNTIFS(SITE,$B10,AGE,D$6)

    Just a thought

    Capture.JPG
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Damn......This is a cunning piece of work . Its going to take me a bit to get my head around it.
    On the Data sheet you've used the labels to provide the criteria for Average Ages and Unit Average ages, pretty!
    However on Sheet2 I'm a bit lost, not having used ISERROR, INDEX and MATCH before. Would you mind giving me a bit of an overview on the formula please.

    Is there a reward system for great responses?

    cheers.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Have a look here http://www.contextures.com/xlFunctions03.html. Debra runs one of the best sites for excel education that I've seen.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I was more thinking about a pivot table.
    Attached Files Attached Files

  6. #6
    Thanks for that. Had a quick look. Mind boggling it is

    cheers

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    However on Sheet2 I'm a bit lost, not having used ISERROR, INDEX and MATCH before. Would you mind giving me a bit of an overview on the formula please.
    =IF(ISERROR(H5),"Age Not Recorded",INDEX($L$2:$M$12,MATCH(H5,$L:$L,1),2))
    Sure, in pseudo-code

    Using J5 with Age=34:

    If H5 is an Error Then (ISERROR)

    Put "Age Not Recorded" in J5

    Else

    Look for (MATCH) the row number of the first value in L:L that is >= (because of the '1' in the 3rd position) the Age. Row 5 (30 & 26-30)

    Use that value (5) as row and 2 as column into the array L2:M12

    Put "31-35" in J5

    End If
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Hey, thanks for the explain and for the assistance. You've been a great help and given me another way to look at things excel

    I'll mark this thread as solved.

    Until my next plea for help,

    cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •