+ Reply to Thread
Results 1 to 11 of 11

counting variables in a loop

  1. #1
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    counting variables in a loop

    I'm looking for a way to count how many times my variable p changes so that I can select the value of p that changes the most. I've google and googled but havent come up with the answer so i'm back here increasing my post count.

    the only reason Cells(Rows.Count, "A").End(xlUp).Offset(1).Resize(, 4).Value = Array(x, y, z, p)
    is in there is so I can see whats going on. I'd like to learn how to do this both by counting the p values in the worksheet and how to do it without using the worksheet.

    Please Login or Register  to view this content.
    Last edited by Symplystyc; 06-26-2011 at 08:29 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: counting variables in a loop

    Hi Sumplyustyc,

    In your code it looks like p goes from 12 to 100 and that would be too easy an answer.

    How about doing this. Put a zero in cell(2,"AA") or somewhere on a sheet. Everytime the If part is true simply do:
    Please Login or Register  to view this content.
    When you run a routine you can then see how many times the If part of the code was executed.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  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
    44,464

    Re: counting variables in a loop

    What do you mean, "how many times the variable p changes"?

    The variable p is in a loop counter from 12 to 100, so I'm guessing the variable changes 89 times ... 12, 13, 14, ... 97, 98, 99, 100.

    Am I missing something?

    If you want to count the number of times x+y+z = p, put a counter in. A cursory look at the results indicates the larger the value of p, the more variations match p ... so it's always going to be the highest value of p that you want. So don't bother looping.

    Regards
    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
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: counting variables in a loop

    Quote Originally Posted by MarvinP View Post
    Hi Sumplyustyc,

    In your code it looks like p goes from 12 to 100 and that would be too easy an answer.

    How about doing this. Put a zero in cell(2,"AA") or somewhere on a sheet. Everytime the If part is true simply do:
    Please Login or Register  to view this content.
    When you run a routine you can then see how many times the If part of the code was executed.
    I'm just going to 100 for now, once I get this figured out I'm going to 1000
    heres some results that I get

    Please Login or Register  to view this content.
    as you can see the value of p changes (p = 25 2 times and p = 26 3 times) i'd like to find the p value that repeats the most.

  5. #5
    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
    44,464

    Re: counting variables in a loop

    The value of p isn't changing, just the number of times it goes through the lower level loops for x and y. I think what you're saying is how many times does p match a combination of x, y and z.

    As I said, it looks as though the higher the value of p, the more combinations of x, y and z will match it ... but that was wrong ;-( The value seems to oscillate.

    The first highest value is 33 for p = 195 but it is also 33 for p = 203. The count never goes above zero after p=341.

    Have a look at the attached sample workbook.

    Regards
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: counting variables in a loop

    Quote Originally Posted by TMShucks View Post
    The value of p isn't changing, just the number of times it goes through the lower level loops for x and y. I think what you're saying is how many times does p match a combination of x, y and z.

    As I said, it looks as though the higher the value of p, the more combinations of x, y and z will match it ... but that was wrong ;-( The value seems to oscillate.

    The first highest value is 33 for p = 195 but it is also 33 for p = 203. The count never goes above zero after p=341.

    Have a look at the attached sample workbook.

    Regards
    yes, thanks for figuring out what I wanted to say
    I looked at your workbook and it looks somethings wrong with the way it counts the farther down you go.

    Please Login or Register  to view this content.
    Do you know of a way to do this without using the worksheet?

  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
    44,464

    Re: counting variables in a loop

    In what way does
    it looks somethings wrong with the way it counts the farther down you go
    Outputting to the sheet is a useful diagnostic tool. Unless I am mistaken, which is not unheard of, there do not appear to be any combinations of x, y and z that match p after p=340.

    The following code uses some stored variables and counters rather than outputting to the sheet. The result's the same.

    Please Login or Register  to view this content.


    Regards

  8. #8
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: counting variables in a loop

    Quote Originally Posted by TMShucks View Post
    In what way does



    Regards
    I guess I dont understand where those zero's come from.

    for example when x + y + z = 76 there are 9 different ways x,y, and z can be expressed but your count column shows only zeros.

    Whats funny is my wife was just cutting my hair and your counter looks almost exactly like I imagined I needed to do.
    So thank you very much for doing that its exactly what I wanted.
    Last edited by Symplystyc; 06-26-2011 at 08:18 PM.

  9. #9
    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
    44,464

    Re: counting variables in a loop

    for example when x + y + z = 76 there are 9 different ways x,y, and z can be expressed but your count column shows only zeros.
    No, it doesn't ....look at columns F and G, row 66: p unique = 76, count = 9

    p unique count
    12 1
    13 0
    14 1
    15 1
    16 1
    17 1
    18 1
    19 2
    20 1
    21 3
    22 2
    23 2
    24 2
    25 2
    26 3
    27 3
    28 2
    29 4
    30 2
    31 5
    32 2
    33 5
    34 3
    35 4
    36 5
    37 3
    38 5
    39 5
    40 5
    41 5
    42 4
    43 6
    44 5
    45 5
    46 7
    47 4
    48 7
    49 4
    50 9
    51 6
    52 6
    53 8
    54 5
    55 9
    56 6
    57 8
    58 7
    59 8
    60 7
    61 9
    62 7
    63 10
    64 7
    65 10
    66 6
    67 11
    68 8
    69 9
    70 11
    71 8
    72 10
    73 10
    74 10
    75 10
    76 9
    77 13
    78 8
    79 12
    80 12
    :

    Regards

  10. #10
    Registered User
    Join Date
    06-07-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: counting variables in a loop

    Quote Originally Posted by TMShucks View Post
    No, it doesn't ....look at columns F and G, row 66: p unique = 76, count = 9




    Regards
    gotcha, I was looking at it wrong.

    Thanks again

  11. #11
    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
    44,464

    Re: counting variables in a loop

    You're welcome. Thanks for the rep.

+ 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