VBA Bubble Sort Method on Values Formatted as Currency

  • Good morning,


    I have been using the bubble sort method with some success for sorting arrays etc. I have however run into a bit of a snag where it appears for some reason that if the cells containing the values that I am building my array with are formatted as currency, or accounting, then the sort "fails" and just spits out the array as it was originally built.


    Any idea why? or how this could be resolved?


    Thanks,

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    What's the code you are using for the bubble sort?

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Hey Rory,


    See below:


  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Use CDbl instead of Val (Val will return 0 due to the currency symbol) or use Value2 to populate the array you pass to the function

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Doesn't seem to like the existence of blank cells, and will give a type mismatch error. Any suggestions?

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Either test the array values using IsNumeric or skip the conversion and populate the array with numbers rather than currency data (using the Value2 property as I mentioned)

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Hey Rory,


    I'm not actually building these arrays, I am pulling Autofilter criteria into an array, so unless I'm mistaken I don't have the Value2 option.


    To your first point, if I understand, I should test the array values before I compare them, and if the Isnumeric returns false...then what, assume the non-numeric value is 0 perhaps?

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Quote from powera86;634095


    I'm not actually building these arrays, I am pulling Autofilter criteria into an array


    Say what??


    Quote

    To your first point, if I understand, I should test the array values before I compare them, and if the Isnumeric returns false...then what, assume the non-numeric value is 0 perhaps?


    Depends - how do you want them treated?

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    Lol...


    This is what is building the array to be tested:


    And I would ideally want blanks treated as 0's

  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    This seems to have fixed it...but some feedback would be appreciated


  • Re: VBA Bubble Sort Method on Values Formatted as Currency


    I'd change

    Code
    strCri1 = strCri1 & "," & CLng(CDate((rng.Text)))


    to

    Code
    strCri1 = strCri1 & "," & IIF(rng.Value2="",0,rng.Value2)

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!