Prevent my VBA Sorting Code stop until last empty column, Its keep looping sorting everything

AndyJR

Board Regular
Joined
Jun 20, 2015
Messages
90
Good morning All,

I have a vba code that sort 60 pairs of Columns Most to least
For example :
It sort Most to Least the Column A3:A38 Based on value of Column B3, Sort next column C3:C38 Based on Values of D3:D38 and so on.. (is supposed to stop after 50 pairs of columns when find the last empty column, but is not. it's keep like looping.
The code is doing the job but it keep going and sort whatever data that is not supposed to be sorted. sometime seems that stop working but is not, (keep finding data to sort)

Please i need to Add the right snipped, can someone help me?

This is the Code

Code:
Sub Sort_M2L_Sheet1() ''To Change Start Column
'For i = 1 To LC Step 2 This 1 means column A and LC means last row.
'If you want to start column J, you can change from = 1   to   = 10


Dim LC As Long, LR As Long, i As Long
Dim wsR As Worksheet
Set wsR = Sheets("Sheet1")
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


Thanks!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If you only want 50 pairs of cols, then try this
Code:
With wsR
    For i = 1 To 100 Step 2
        LR = .Cells(Rows.Count, i).End(xlUp).Row
        With .Sort
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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