+ Reply to Thread
Results 1 to 15 of 15

Sorting Variables in an Array (VBA Code ONLY)

  1. #1
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Sorting Variables in an Array (VBA Code ONLY)

    I am not sure how to accomplish sorting the variables in the array. They need to be sorted from highest value to lowest. Then the allowance is calculated (100% of highest, the rest at 50%).

    Please Login or Register  to view this content.
    The attached spreadsheet outlines the steps and gives an example
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Sorting Variables in an Array (VBA Code ONLY)

    lloydgodin,

    Something like this?
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    Exactly what I was looking for. Thank you.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Sorting Variables in an Array (VBA Code ONLY)

    You're very welcome

  5. #5
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    I finally ran across an error. There may be instances where the value of the cpt code is the same. The macro then puts both values at 100% which would be incorrect.

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Sorting Variables in an Array (VBA Code ONLY)

    lloydgodin,

    Made slight adjustment to code (included the bMaxed boolean variable) which should correct that issue:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    Very nice. Thank you!

  8. #8
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    So I've used your code for the basis of a similar project. It works if the code with the highest value is entered first, but if I reverse it then it doesn't.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Sorting Variables in an Array (VBA Code ONLY)

    lloydgodin,

    I'm not sure what you mean. I also am unsure what the contiguous is supposed to do (there's no test for it, it's just always true?). The sort takes place after the array is output to the sheet. Does this not have to do with sorting?

  10. #10
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    The contiguous part is a test I need to do later. If true the allowance for the lower value is worth 50% of fee sched. If false the lower value is worth 75% of fee sched.

    The sorting parting irrelevant for what I need on this project. I did notice this morning that it was after the calculations.

    I apologize for not being more clear. When I enter two hcpc codes it figures out the rrv value correctly (100% & 50%) if the hcpc entered first is already the highest valued code. But if I enter a lower value hcpc first, it figures everything at 100%.

    I.e if the rrv value of 72170 is 1.45 and rrv calue of 71010 is 1.28 and I enter (in data entry process) 72170 first & 71010 it figures the rrv values at 1.45 & 0.64 (which is correct). If I enter 71010 first & 72170 second, it figures the rrv values at 1.28 & 1.45 (which would be incorrect).

  11. #11
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)


  12. #12
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Sorting Variables in an Array (VBA Code ONLY)

    Alright, after looking at your posted code snippet, I think the problem is the location of the "rMaxed = False" line. From what I can tell, you have that as part of the loop, instead of before the loop occurs. It should be something like this:
    Please Login or Register  to view this content.


    See how the "rMaxed = False" is outside of any loops? That way it's only false until it becomes true, instead of being set to false each time i increases.

  13. #13
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    Yes, I see that. I moved it (keeping my code) and it halved the 2nd value but the issue of highest/lowest vs 1st/2nd entered still remained.

    I put the above code it, but i is already defined and changing it returned no values. The problem is that it's already in a loop defined by i. The original loop is for the whole array (all revcodes) where this particular is a subset of the array (rev codes 320 to 359).

  14. #14
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Sorting Variables in an Array (VBA Code ONLY)

    You're going to have to split it into 2 loops. The first loop gathers the vlookup values and determines which one is the largest. The second loop goes through the values and halves them if they are not the largest. It can't know what the largest value is until it has obtained all of the values from the first loop. Trying to do both in a single loop will only provide incorrect results. See the code I posted for what I mean. It has two loops doing what I've described.

  15. #15
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Sorting Variables in an Array (VBA Code ONLY)

    Gotcha. It worked. I appreciate your help.

+ 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