Sort Macro with relative sort key

Texas Longhorn

Active Member
Joined
Sep 30, 2003
Messages
493
Hi All,

I have a worksheet with a few hundred columns of data. The data is paired in groups of two columns with equal rows (i.e. Columns A and B have 1000 rows, Columns C and D havd 990 rows, Columns E and F have 1115 rows, etc...they're paired). I'm trying to write some code to sort each two-column group by the pair's second column (i.e. Sort A1:B1000 by values in column B; sort C1:D990 by values in column D; sort E1:F1115 by values in column F, etc).

I wrote the following which highlights an error with the Sort line.

Code:
Sub SortByGroup()

    'Application.ScreenUpdating = False
    
    
    For i = 1 To 367
    
        ActiveCell.Range("A1:B1").Select
        Range(Selection, Selection.End(xlDown)).Select
        
        ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key1:=Range("B1"), Order:=xlAscending, Header:=xlYes
        Selection.Offset(0, 2).Activate
        
    Next i
    

    'Application.ScreenUpdating = True

End Sub

Any help would be much appreciated.

Thanks,

Bill
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
For those curious, this works (thought it could certainly be improved):

Code:
Sub SortByGroup()

    Application.ScreenUpdating = False
    
    
    For i = 1 To 367
    
        ActiveCell.Range("A1:B1").Select
        Set sortBlock = Range(Selection, Selection.End(xlDown))
        
        sortBlock.Sort Key1:=ActiveCell.Range("B1"), Order1:=xlAscending, _
        Header:=xlYes
        Selection.Offset(0, 2).Activate
        
    Next i
    

    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,568
Messages
6,179,600
Members
452,927
Latest member
whitfieldcraig

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top