+ Reply to Thread
Results 1 to 8 of 8

Setting variable data-sort ranges

Hybrid View

  1. #1
    Registered User
    Join Date
    08-07-2008
    Location
    Northants, England
    Posts
    18

    Setting variable data-sort ranges

    I have a macro to select a range based on various criteria either being true or false. Whichever range is selected, I would then like to datasort that range ONLY. Unfortunately i can't seem to get my code to reference a variable area to data sort.

    Sub DATA_SORT()
    
    
        Dim rng     As Range
    
        If Range("I11") = True Then
            Set rng = Range("K12:AR190")
        ElseIf Range("I10") = True Then
            Set rng = Range("K11:AR190")
        ElseIf Range("I9") = True Then
            Set rng = Range("K10:AR190")
        ElseIf Range("I8") = True Then
            Set rng = Range("K9:AR190")
        ElseIf Range("I7") = True Then
            Set rng = Range("K8:AR190")
        ElseIf Range("I6") = True Then
            Set rng = Range("K7:AR190")
        ElseIf Range("I5") = True Then
            Set rng = Range("K6:AR190")
        ElseIf Range("I4") = True Then
            Set rng = Range("K5:AR190")
        Else
            Set rng = Range("K4:AR190")
        End If
    
        rng.Sort Key1:=Range("AG4"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End Sub
    The part that is causing me problems is:

       rng.Sort Key1:=Range("AG4"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    A friend suggested changing the Range("AG4") part to Range("AG"+rng) or possibly Range("AG".rng) neither of which work.

    Appreciate any suggestions you guys may have!

    Thanks
    Forcybo

  2. #2
    Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    Hi forcybo,

    Try this code:

     rng.Sort Key1:=Cells(rng.Row, rng.Column), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    i havnt tested it but may work
    There are only 10 types of people in the world:
    Those who understand binary, and those who don't!

  3. #3
    Registered User
    Join Date
    08-07-2008
    Location
    Northants, England
    Posts
    18
    Hi Stuie,

    Thanks for your input! Unfortunately the code you have suggested still sorts from row 4 but now also sorts by column K (the first column in the range) rather than column AG as before.

    I'm slowly going bald from all the hair being pulled out over this lol

  4. #4
    Forum Contributor Stuie's Avatar
    Join Date
    09-17-2006
    Location
    Suffolk, UK
    MS-Off Ver
    Excel 2003/2007
    Posts
    432
    Lol, spare the hair, you will get there eventually.

    are you looking for it to always sort by AG?

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    How are you getting th True value?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Registered User
    Join Date
    08-07-2008
    Location
    Northants, England
    Posts
    18
    Stuie - yes mate, always looking to data sort by AG (for this sheet AG is actually a customer's requested delivery date).

    Royuk - the true value is determined by a tickbox. When the tickbox is checked (TRUE) it enters a value of X into the corresponding cell in Column I. Do you think this might be the issue? In a previous incarnation of this macro i played with changing the 'True' value to "X" and it didn't seem to make much of a difference.

+ 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