With short VBA Code How to Sort pairs of columns using loop until last empty column (120 total)

AndyJR

Board Regular
Joined
Jun 20, 2015
Messages
90
Hi,

It may sound ridiculous what i'm asking, but i made a macro to sort in descending order the first 2 pairs of column (among 120 sets), But it seems that if i sort 120 sets of columns the macro is going to be huge, and that's why i'm requesting help to see if i can get a VBA or code with decent size.

For example first Pair of columns:
A3 have Store Number B3 Sales for Week1 (Then Next Column C3 Store and Column D3 Sales Week2 and so on..)
I need to sort Based on Week sales and repeat the same task for the next pair of columns until last empty column
_____A_______B___
A1 _Store___Sales_
A2
_Numb___Week1_
A3_Store 4__ 2000.00
A4_Store 7__ 1895.00
A5_Store 1__ 1800.00
A6_Store 3__ 1600.00
A7_Store 5__ 1585.00
A8_Store 6__ 1250.00
A9_Store 2__ 1000.00

What I need is to sort in Descending Order Based on Week sales and repeat the same task for the next pair of columns (C3 and D3) until last empty columns (as A3 and B3 example).

This is the Macro that i make Just For Column A and B, as can you observe is big for 2 columns and i don't know yet if the file is going to be slow.. :(

Code:
Sub SortSample()'

    Range("A2:B9").Select
    Selection.AutoFilter
    ActiveWorkbook.Worksheets("Report").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Report").AutoFilter.Sort.SortFields.Add Key:=Range( _
        "A2:B9"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("report").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Selection.AutoFilter
    
End Sub


Thank you so much!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hope this helps.

Code:
Sub SortSample() '
Dim LC As Long, LR As Long, i As Long
Dim wsR As Worksheet
Set wsR = Sheets("report")
With wsR
    LC = .cells(3, Columns.count).End(xlToLeft).column
    For i = 1 To LC Step 2
        LR = .cells(Rows.count, i).End(xlUp).row
        With .Sort
            .SortFields.Clear
            .SortFields.Add Key:=wsR.cells(2, i + 1)
            .SetRange wsR.Range(wsR.cells(2, i), wsR.cells(LR, i + 1))
            .Header = xlYes
            .Orientation = xlTopToBottom
            .Apply
        End With
    Next
End With
End Sub
 
Upvote 0
Hi Takae,

Thank you for the fast response, and thank for the code..

I have 2 question,

1-. The code is sorting in ascending Order, Which line do i have to change to make sort Descending? (sorry ignorance lol) but i can't see the line Order1:=xlDescending

2-. Which Number do i have to change if, (let say) if i decide to start sorting on column J3 and K3 and not A and B? (I mean Column 10 and 11?)


Thank you so much, appreciate !!
 
Last edited:
Upvote 0
Sorry, it lacked your order. Please try again.

For i = 1 To LC Step 2
This 1 means columnA and LC means last column.
If you want to start columnJ, you can change 1 to 10.

Code:
Sub SortSample() '
Dim LC As Long, LR As Long, i As Long
Dim wsR As Worksheet
Set wsR = Sheets("report")
With wsR
    LC = .cells(3, Columns.count).End(xlToLeft).column
    For i = 1 To LC Step 2
        LR = .cells(Rows.count, i).End(xlUp).row
        With .Sort
            .SortFields.Clear
            .SortFields.Add _
            Key:=wsR.cells(2, i + 1), _
            Order:=xlDescending
            .SetRange wsR.Range(wsR.cells(2, i), wsR.cells(LR, i + 1))
            .Header = xlYes
            .Orientation = xlTopToBottom
            .Apply
        End With
    Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,430
Members
449,158
Latest member
burk0007

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