+ Reply to Thread
Results 1 to 3 of 3

Sort a range using a variable

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2009
    Location
    Cardiff, Walse
    MS-Off Ver
    Excel 2003
    Posts
    2

    Sort a range using a variable

    I have a number of different columns i want to sort on the same worksheet but i want to sort each collumn by a different range.

    currently im using this code

    g1summary.Range("R5:U10").Sort Key1:=g1summary.Range("U5"), Order1:=xlDescending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    But i want it so i can use a varible range so i can put the code within a loop

    ive tried this but it doesnt work...


    g1summary.Range(Cells(5, n), Cells(norows, n+3)).Sort Key1:=g1summary.Range(cells(5, n+3), cells(norows, n+3), Order1:=xlDescending, Header:=xlNo, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    n is the variable representing the column number



    a larger overview of my code is..

    Do Until shopcount > 8
    i = 14
        Do Until i > 31
            module = sc1.Cells(i, "C")
            acthours = sc1.Cells(i, shopcount)
            stdhours = sc1.Cells(i, shopcount + 1)
            shop = sc1.Cells(12, shopcount)
    
            'do while std hours is blank
            Do While stdhours = "" And i < 32
            
          
                i = i + 1
            
                If i = 32 Then
                Else
                   
                    module = sc1.Cells(i, "C")
                    acthours = sc1.Cells(i, shopcount)
                    stdhours = sc1.Cells(i, shopcount + 1)
                    shop = sc1.Cells(12, shopcount)
                    
                End If
            Loop
        
        'print loop
        
        n = 18
        exitloop = False
        
        Do Until n > 83 Or exitloop = True
            module2 = g1summary.Cells(3, n)
            shop2 = g1summary.Cells(2, n)
            norows = g1summary.Cells(1, n + 1) + 2
            
            If module = module2 And shop = shop2 And stdhours <> "" Then
                g1summary.Cells(norows, n) = ESN
                g1summary.Cells(norows, n + 1) = acthours
                g1summary.Cells(norows, n + 2) = stdhours
                g1summary.Cells(norows, n + 3) = Date
                exitloop = True
                
    'sort summary data
    '!!!!!!!!!!!!!!!!!!!!!!!!
    'code in question
    'instead of using this code is there a way of using a variable range?
                    
        g1summary.Range("R5:U10").Sort Key1:=g1summary.Range("U5"), Order1:=xlDescending, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
            
            End If
            
    
            
        'next shop on g1 summary
        n = n + 4
        'jump gap
        If n = 78 Then
        n = 79
        End If
        Loop
                                'End of print loop
    i = i + 1
    Loop
     
    'next shop on sc3
    shopcount = shopcount + 3
    Loop

    Thanks for any help
    Last edited by iAlex; 10-13-2009 at 05:43 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Sort a range using a variable

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    10-13-2009
    Location
    Cardiff, Walse
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Sort a range using a variable

    sorry i didnt realise.
    Hope the edited post now meets the forum rules.

    Thanks,
    Alex

+ 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