+ Reply to Thread
Results 1 to 11 of 11

Calculating a percentage of "Y" and "N" in dynamic cell range

  1. #1
    Registered User
    Join Date
    12-26-2015
    Location
    Las Vegas
    MS-Off Ver
    2013
    Posts
    56

    Calculating a percentage of "Y" and "N" in dynamic cell range

    ::high five:: whats up everybody,

    Again, I bow in your general direction...

    Attached is the data and VBA I am working on.

    I have a few columns of data with "Y" and "N".

    I need the percentage of "Y" for columns "I" through "L" to be displayed in percentage format on the right upper side of the sheet. (as noted in the macro notes)

    If you run the macro it will display what is already done. It would be maximum awesomeness if it looped for each sheet. (only one attached but final has 24)

    Capture.PNG

    Thanks in advance for any help!!!

    Inky
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    Technically you can just apply for formula like you did for Avg duration, except that the formula will look like =COUNTIF($I$2:$L$3535,"Y")/COUNTA($I$2:$L$3535)

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    46,964

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  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: Calculating a percentage of "Y" and "N" in dynamic cell range

    Hi and welcome to the forum.

    Using a macro to accomplish this task seem rather OTT, particularly one which involves a loop. Loops are inherently slow and should only be used if there's no standard Excel functions that you can use. In this case you can use simple COUNTIF() and COUNTA() functions.

    First create a dynamic range name which will adjust automatically to cater for any number of rows that you may add. I created one called
    'Data_YN' and defined this in the name manager as

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Now all you need is to put this formula in N1, or anywhere for that matter
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Make sure when you define the name that the Scope is set to Sheet level. Then assuming you copy the sheet to create the other 23 the same name will be present on each one but scoped to the sheet its on. If the 24 sheets are already in place then put the following line of code in a macro that iterates through all the sheets

    Please Login or Register  to view this content.
    late edit...just spotted an error. ....wait one... Now Corrected
    Last edited by Richard Buttrey; 01-29-2016 at 07:33 PM.
    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.

  5. #5
    Registered User
    Join Date
    12-26-2015
    Location
    Las Vegas
    MS-Off Ver
    2013
    Posts
    56

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    JieJen,

    Unfortunately I was unable to see how to duplicate what your were saying to do to my other code...

    Dan

  6. #6
    Registered User
    Join Date
    12-26-2015
    Location
    Las Vegas
    MS-Off Ver
    2013
    Posts
    56

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    TMS... it was amazing how fast you did that!!!

    I had to make one change...

    lLR = Range("H" & Rows.Count).End(xlUp).Row

    I had to change xlUp to xlDown...

    xlUp was giving me the range of

    =AVERAGE(H2:H3461)

    xlDown was giving me the range of

    =AVERAGE(H2:H3535)... which is the correct range.

    But once I did that it seemed to work perfectly! As far as I can tell that is!!!

    Thanks!!!

    Dan

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    46,964

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    Oops, missed a dot or two ...

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

    Regards, TMS

  8. #8
    Registered User
    Join Date
    12-26-2015
    Location
    Las Vegas
    MS-Off Ver
    2013
    Posts
    56

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    The sheets start out with about 30k to 100k of rows depending on the month. Only about 20 columns though. Then the other parts of the macro I have run sort out into 24 other pages in which I needed the percentages for. LAS being one.

    The loop will stop me from any manual anything.

    You seem like you would be a really good teacher!

  9. #9
    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: Calculating a percentage of "Y" and "N" in dynamic cell range

    Quote Originally Posted by InkyDrinky View Post
    TMS... it was amazing how fast you did that!!!

    I had to make one change...

    lLR = Range("H" & Rows.Count).End(xlUp).Row

    I had to change xlUp to xlDown...

    xlUp was giving me the range of

    =AVERAGE(H2:H3461)

    xlDown was giving me the range of

    =AVERAGE(H2:H3535)... which is the correct range.


    Dan
    Hi Dan,

    There seems something wrong there. I see no reason why Trevor's xlUp would return 3461 as the last row. The technique of always selecting the very last cell and doing an xlUp is generally regarded as the safest way to find the last row, particularly where there might be blanks in the column since an xlDown would in that case not return the last row.

    If all the cells are contiguous with some value in each then an xlUp and an xlDown will return the same value.
    It might be worth double checking what was happening with your particular example.


    ...late edit... I see Trevor has just responded. However it's still better practice to use xlUp for the reason stated.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    46,964

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    @Richard: my error. Probably means the first/active sheet only had 3461 rows. As there was only one sheet, that aspect didn't get tested. And even if I'd copied the sheets, they'd probably just have been straight copies.

  11. #11
    Registered User
    Join Date
    12-26-2015
    Location
    Las Vegas
    MS-Off Ver
    2013
    Posts
    56

    Re: Calculating a percentage of "Y" and "N" in dynamic cell range

    Richard Buttrey thank you and I will keep that in mind.

    Learning a lot from you guys!!

+ 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] VBA help needed to remove all "/" then replace with "-" from cell "B3"and "B5"
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2014, 02:11 PM
  2. [SOLVED] If Not ActiveSheet.Range("A1").Value Like "apple" Then MsgBox "Error"
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-18-2014, 02:16 PM
  3. Replies: 5
    Last Post: 01-23-2014, 11:02 AM
  4. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  5. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  6. [SOLVED] If there is any text in column "A$" on "sheet1" then move cell to column "A$" on "sheet2"
    By ckgeary in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-27-2013, 08:28 PM
  7. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  8. [SOLVED] use variable in Workbooks("book1").Worksheets("sheet1").Range("a1"
    By Luc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2005, 04: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