+ Reply to Thread
Results 1 to 39 of 39

Why doesn't this VBA code count the frequencies of each value?

  1. #1
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Why doesn't this VBA code count the frequencies of each value?

    I am trying to write a code that will:

    Sort elements (Call tri1) in an array - (note that the array proceeds from a range (dimension 10,1) of values in excel.

    Create three intervals of size 2.

    Assign each value to its corresponding interval.

    Count the number of values in each interval, and hence, I will have the frequency.

    I wrote this code, but it returns values that do NOT correspond to the number of occurrences. Would anyone be able to tell what I might be missing? Or maybe something is just plain wrong in the For Each loop with If .. Then statements?


    The column of values I am working with on excel is:
    2
    3
    4
    5
    7
    8
    1
    2
    4
    1

    Please Login or Register  to view this content.
    And the bubble sorting:

    Please Login or Register  to view this content.
    Last edited by seigna; 04-14-2013 at 12:26 PM. Reason: Missing elements

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code in [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    PS There's also some code missing, a sub/function called tri1.
    Last edited by Norie; 04-14-2013 at 11:42 AM.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Done, thanks!

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    What result are you expecting and what result are you getting?

    There are a couple of things that don't quite seem right, both of which are in these lines of code.
    Please Login or Register  to view this content.
    First thing is that you are referring to a worksheet with the codename Sheet3 whereas in the rest of the code you've used Worksheets("Sheet3").

    Second, you are resizing Destination1 to 3 rows and 1 column while plaga2 is an array with dimensions 1 to 10, 1 to 1.

  5. #5
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    I might have forgotten things through editing... plaga 2 is supposed to hold only 3 rows in one column.

    Well, I am looking for 3 intervals of size (8-1)/3. I would like each row in plaga2 to contain the number of elements in each interval. So, in the first one I would get 1,1,2,2,3,3 etc. which would amount to 6 occurrences. Yet, with my code I get a 7. And my frequences add up to a number higher than the number of values, which in total should be 10 :/

  6. #6
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    I have based my code for the frequences - note that I only care about frequences, I don't want any histogram - on the code on the following site. http://sitestory.dk/excel_vba/histogram-vba.htm

    I have also tried this: http://www.anthony-vba.kefra.com/vba/vba10.htm (Bottom code) with:

    Please Login or Register  to view this content.

    But nothing works

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    Aren't there 5 items in the first interval, 3 in the second and 2 in the third?

  8. #8
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Yes, you're right! Im sorry, didn't check right

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    I came up with this which seems to work to some extent.
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    True! Exceot it returns them in a descending order :/ But it is exactly that!

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    I couldn't get the Match part of the code to work properly with the interval values in ascending order.

    It should be easy to turn things round.

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    I think I see why it is so, you are starting at the max value

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    I can't really see what you're doing over there.. :S

    Over here

    Please Login or Register  to view this content.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    Yes, but for the Match to work arrintervals needs to be in descending order.

  15. #15
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Mmmmmm, okay, and is there any way in which I could turn that process around?

  16. #16
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    The thing is I will have to cumulate these frequencies later and graph a distribution.. so I can't leave them in a descending order. But then, I think I would have to change the way I display them in excel (the order), isn't that right? Except, the lines of code you added with For 3 to 1 Step -1 don't really do that :S

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    The code won't work unless arrintervals is in descending order.

    If you want to display the result in ascending order use the code I posted in post #11.

  18. #18
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Did that and it doesn't change anything :/ Does it work when you test it?

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    Yes.

    You did remove this code didn't you?
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Yes and it didn't change the order it displays..

  21. #21
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Sorry, no, it did! Thank you so much!!

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    Can you post the exact code?

    Or even better upload a sample workbook.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  23. #23
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Okay, here it comes.
    Attached Files Attached Files

  24. #24
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    When I run the code in that workbook the intervals are in ascending order.


    3.333333333 5
    5.666666667 3
    8 2

    What exactly were you expecting?

  25. #25
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    It's fine! I was expecting exactly this! Thanks

  26. #26
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    I was wondering.. cause I changed a bit the number of data I was dealing with to:

    Please Login or Register  to view this content.
    The problem is now I get an interval where there are no values and it leaves it blank :/ ..I would like to make it 0 though.. Is there such an option to do this?

  27. #27
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    I get this ...

    Please Login or Register  to view this content.

  28. #28
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    How many intervals now?

    What you can do is change the ReDim of plaga2 to declare the array as Long.
    Please Login or Register  to view this content.
    The 3 would need to be replaced by the no of intervals you now have.

    PS You'll also need to delete the initial declaration of plaga2.
    Last edited by Norie; 04-14-2013 at 07:06 PM.

  29. #29
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Now there are 5 of them. I replaced all of them, and one of them returns blank. It gives the interval, but Match returns a blank, yet I'd rather have a zero in there..

  30. #30
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    VBA tells me "Can't change data types of array elements

  31. #31
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    See what I added to the previous post when I edited it.

  32. #32
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Can you pls tell me whose post?

  33. #33
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Ok, I've seen it! Im going to see tomorrow am if it works.

  34. #34
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    I've updated the code, added a NoIntervals variable to set the number of intervals and changed the ReDim of plaga2.
    Please Login or Register  to view this content.

  35. #35
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Hmmm...so why didnt excel let me define plaga2 as Long even if i did the same - except i had 1 to 5, not Nointervals, which is maybe the answer..

  36. #36
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    Did you remove the original Variant declaration for plaga2?

  37. #37
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Yes, I tried it. I didn't use NoIntervals, though.

  38. #38
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Why doesn't this VBA code count the frequencies of each value?

    So the code works?

  39. #39
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Why doesn't this VBA code count the frequencies of each value?

    Yes. Thank you! Got a question, would you be able to check out my post about cumulative sums pls?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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