+ Reply to Thread
Results 1 to 23 of 23

Sorting VBA Simplification

Hybrid View

  1. #1
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Sorting VBA Simplification

    I have written a VBA module to Sort Filtered Unique data in a specific location. The problem is, I do the same code four times.
    Is there a more efficient way to use the same sorting method without having to do the same code four times?
    I have supplied a copy of my code for all to review:

    
    'Sort Unique Values: TestGrp
    Range("SgDV_TestGrp").Sort Key1:=Range("SgSort_TestGrp"), Order1:=xlAscending, Header:= _
    xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    
    'Sort Unique Values: Product
    Range("SgDV_Product").Sort Key1:=Range("SgSort_Product"), Order1:=xlAscending, Header:= _
    xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    
    'Sort Unique Values: Machine
    Range("SgDV_Machine").Sort Key1:=Range("SgSort_Machine"), Order1:=xlAscending, Header:= _
    xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    
    'Sort Unique Values: SampleType
    Range("SgDV_SampleType").Sort Key1:=Range("SgSort_SampleType"), Order1:=xlAscending, Header:= _
    xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

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

    Re: Sorting VBA Simplification

    Perhaps.
    arrRanges("SgDV_TestGrp", "SgDV_Product", "SgDV_Machine", "SgDV_SampleType")
    
    For I = LBound(arrRanges) To UBound(arrRanges)
    
       'Sort Unique Values: TestGrp
       Range(arrRanges(I)).Sort Key1:=Range(arrRanges(I)), Order1:=xlAscending, Header:= _
            xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    Next I
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    Thanks Norie. I have a question about your array code example.
    If you look at the original code I supplied there are two named ranges per sort code:

    Sort 1: "SgDV_TestGrp", "SgSort_TestGrp"
    Sort 2: "SgDV_Product", "SgSort_Product"
    Sort 3: "SgDV_Machine", "SgSort_Machine"
    Sort 4: "SgDV_SampleType", "SgSort_SampleType"

    How would I handle this? Your example only shows the SGDV named ranges.

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

    Re: Sorting VBA Simplification

    Oops did't notice that.
    arrRanges("TestGrp", "Product", "Machine", "SampleType")
    
    For I = LBound(arrRanges) To UBound(arrRanges)
    
       'Sort Unique Values: TestGrp
       Range("SgDV_" & arrRanges(I)).Sort Key1:=Range("SgSort_" & arrRanges(I)), Order1:=xlAscending, Header:= _
            xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    Next I

  5. #5
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    OK. Obviously I'm doing something wrong, I just don't know what it is. I modified the code but it still doesn't work. I get a "Sort Method of Range class Failed" error.
    Here is my modified code:

    
    Sub UniqueValues_SG2()
    
    Dim arrRanges As Variant, i As Long
    
        arrRanges = Array("TestGrp", "Product", "StartMachine", "SampleType")
    
        For i = LBound(arrRanges) To UBound(arrRanges)
            
            Range("SgDV_" & arrRanges(i)).Sort Key1:=Range("SgSort_" & arrRanges(i)), _
            Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBotom, DataOption1:=xlSortNormal
            
        Next i
    
    End Sub

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

    Re: Sorting VBA Simplification

    Why do you have StartMachine in the array?

    Shouldn't it be Machine?

  7. #7
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    Your right. My mistake. But even after I correct that type, I still get the "Sort Method of Range class Failed".

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

    Re: Sorting VBA Simplification

    As far as I can see all the names are right, has anything else been changed?

  9. #9
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    No. Nothing.

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

    Re: Sorting VBA Simplification

    Can you upload an example workbook?

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

  11. #11
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Sorting VBA Simplification

    OK. Here is the file.
    Attached Files Attached Files

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

    Re: Sorting VBA Simplification

    Sorry but I'm at a loss here.

    I tried a few variations, for example the below code, but still same error.
    Dim arrRanges As Variant
    Dim arrSort As Variant
    Dim i As Long
    
        arrRanges = Array("SgDV_TestGrp", "SgDV_Product", "SgDV_Machine", "SgDV_SampleType")
        arrSort = Array("SgSort_TestGrp", "SgSort_Product", "SgSort_Machine", "SgSort_SampleType")
    
        For i = LBound(arrRanges) To UBound(arrRanges)
            
            Sheet3.Range(arrRanges(i)).Sort Key1:=Sheet3.Range(arrSort(i)), _
            Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
            Orientation:=xlTopToBotom, DataOption1:=xlSortNormal
            
        Next i

  13. #13
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    How would handle the arrSort variant? Would you use a "For j, Next j" approach? "i" is incremented for arrRanges, but arrSort is not incremented. What are your thoughts?

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

    Re: Sorting VBA Simplification

    The arrays have the same no of elements so you can use i for both of them.

    When i is 0 arrRanges(0) will return "SgDV_TestGrp" and arrSort(0) will return "SgSort_TestGrp".

  15. #15
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    Thank you. I have never used arrays before. I tried searching the web for any explanation on use of arrays and couldn't find anything useful. Your description helps tremendously.
    As for my problem, do you have any ideas?

  16. #16
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    Figured out what the problem was. Look at posting #12 on the last line, the word "Bottom" is misspelled as "Botom". Once this was corrected everything worked.

    Sorry, posted this without realizing that this was pointed out by tigeravatar in posting #16.

    Thanks to everyone who contributed to this thread. I learned alot.
    Last edited by rmoreno; 07-26-2013 at 06:35 PM.

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

    Re: Sorting VBA Simplification

    The problem is that the Orientation argument is mispelled:
    Orientation:=xlTopToBotom
    Should be xlTopToBottom

    Alternatively, since it is an optional argument and that is its default value anyway, you could just delete that argument (and the other optional arguments that are set to their default values) to correct the issue also:
    Sub UniqueValues_SG2()
        
        Dim arrRanges As Variant, i As Long
        
        arrRanges = Array("TestGrp", "Product", "Machine", "SampleType")
        For i = LBound(arrRanges) To UBound(arrRanges)
            Range("SgDV_" & arrRanges(i)).Sort Range("SgSort_" & arrRanges(i)), xlAscending, Header:=xlGuess
        Next i
        
    End Sub
    Hope that helps,
    ~tigeravatar

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

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

    Re: Sorting VBA Simplification

    So that means the code I posted in #4 would work, if the error with the array was fixed.

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

    Re: Sorting VBA Simplification

    Indeed it would!

  20. #20
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Sorting VBA Simplification

    Perhaps you could do it this way - you could play with the strings for the setups:

    Sub SUV(SgDV As String, SgSort As String)
    Range(SgDV).Sort Key1:=Range(SgSort), Order1:=xlAscending, Header:= _
    xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal:End Sub
    
    Call SUV("SgDV_TestGrp","SgSort_TestGrp")
    Call SUV("SgDV_Product","SgSort_Product")
    Call SUV("SgDV_Machine","SgSort_Machine")
    Call SUV("SgDV_SampleType","SgSort_SampleType")
    Last edited by xladept; 07-26-2013 at 03:31 PM. Reason: tidy up
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

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

    Re: Sorting VBA Simplification

    If this is solved then please mark it solved, and spread some rep about.

  22. #22
    Registered User
    Join Date
    09-15-2005
    Posts
    17

    Re: Sorting VBA Simplification

    Don't know how to mark as SOLVED, but this thread is SOLVED.

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

    Re: Sorting VBA Simplification

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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] Formula Simplification
    By brharrii in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-24-2013, 10:46 AM
  2. Formula Simplification
    By nanocrazy in forum Excel General
    Replies: 2
    Last Post: 04-16-2010, 03:14 AM
  3. if statement Simplification
    By sk81681 in forum Excel General
    Replies: 16
    Last Post: 06-05-2009, 05:24 AM
  4. Simplification help
    By Mike Smith NC in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-12-2006, 01:35 PM
  5. [SOLVED] Simplification help
    By Mike Smith NC in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-12-2006, 11:05 AM

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