Need VBA Code to automatically sort on a numerical column highest to lowest when pasting associated data.

JMC57

Board Regular
Joined
Apr 24, 2012
Messages
118
I have an Excel Sheet with columns A through J. There are 1900 rows. The Numerical data to drive the sorting is in Column J. There are formulas in Col K-O. The cells are protected, and these are the same formulas that are copied down and locked.

I need a VBA code to sort rows based on Col J highest to lowest numerical value without impacting the sort of Col K-O

Any help, suggestions or solutions will be appreciated.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
not sure if you have passwords on the protection but, try this

VBA Code:
Sub SortData()
    Dim ws As Worksheet
    Dim lR As Long
    
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' your sheet
    
    ws.Unprotect
    
    lR = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row
    
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("J2:J" & lastRow), SortOn:=xlSortOnValues, _
            Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange Range("A1:O" & lastRow)  ' assuming A as start of columns
        .Header = xlYes  ' assuming you have headers
        .Apply
    End With
    
    ws.Protect
End Sub
 
Upvote 0
Thank you for your response. The last time a worked with VBA was 2013 so I am very rust and was never really that good at it.
Does it matter if I have password protection? I have it when I lock the sheets ?

Set ws = ThisWorkbook.Worksheets("Sheet1") ' your sheet Do I keep ‘your sheet?
lR = ws.Cells(ws.Rows.Count, "J"). Do I change “J” to “J1900”? since there are 1900

.SortFields.Add Key:=Range("J2:J" & lastRow), Do I change “J” to “J1900”? since there are 1900 rows
SetRange Range("A1:O" & lastRow) Do I change tio A2 and O1900
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Need VBA Code to automatically sort on a numerical column highest to lowest
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Thank you for your response. The last time a worked with VBA was 2013 so I am very rust and was never really that good at it.
Does it matter if I have password protection? I have it when I lock the sheets ?

Set ws = ThisWorkbook.Worksheets("Sheet1") ' your sheet Do I keep ‘your sheet?
lR = ws.Cells(ws.Rows.Count, "J"). Do I change “J” to “J1900”? since there are 1900

.SortFields.Add Key:=Range("J2:J" & lastRow), Do I change “J” to “J1900”? since there are 1900 rows
SetRange Range("A1:O" & lastRow) Do I change tio A2 and O1900
not sure if you have passwords on the protection but, try this

VBA Code:
Sub SortData()
    Dim ws As Worksheet
    Dim lR As Long
   
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' your sheet
   
    ws.Unprotect
   
    lR = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row
   
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("J2:J" & lastRow), SortOn:=xlSortOnValues, _
            Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange Range("A1:O" & lastRow)  ' assuming A as start of columns
        .Header = xlYes  ' assuming you have headers
        .Apply
    End With
   
    ws.Protect
End Sub
I entered the suggested code as
Sub SortData()
Dim ws As Worksheet
Dim lR As Long

Set ws = ThisWorkbook.Worksheets("2Cut And Paste ABC report")

ws.Unprotect

lR = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row

With ws.sort
.SortFields.Clear
.SortFields.Add Key:=Range("J2:J1999" & lastRow), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
.SetRange Range("A1:O" & lastRow)
.Header = xlYes
.Apply
End With

ws.Protect
End Sub

I get a Run-Time error 9 Subscript out of rance
 
Upvote 0
I have an Excel Sheet with columns A through J. There are 1900 rows. The Numerical data to drive the sorting is in Column J. There are formulas in Col K-O. The cells are protected, and these are the same formulas that are copied down and locked.

I need a VBA code to sort rows based on Col J highest to lowest numerical value without impacting the sort of Col K-O

Any help, suggestions or solutions will be appreciated.
Note this code is also posted at
Need VBA Code to automatically sort on a numerical column highest to lowest
 
Upvote 0
Try this, sorry my variable was mislabled

VBA Code:
Sub SortData()
    Dim ws As Worksheet
    Dim lR As Long
   
    Set ws = ThisWorkbook.Worksheets("Sheet2") ' your sheet
   
    ws.Unprotect
   
    lR = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row
   
    With ws.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("J2:J" & lR), SortOn:=xlSortOnValues, _
            Order:=xlDescending, DataOption:=xlSortNormal
        .SetRange Range("A1:O" & lR)  ' assuming A as start of columns
        .Header = xlYes  ' assuming you have headers
        .Apply
    End With
   
    ws.Protect
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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