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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,721
Members
449,093
Latest member
Mnur

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