Sort - vba - only sorting one column

shakethingsup

Board Regular
Joined
May 21, 2017
Messages
64
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm learning about sort through vba.

7R0DUm1.png


I want to sort by column E in descending order. So I used this code where lastrow is the last row in the spreadsheet that has data:

Range("A4:K" & lastrow).Sort key1:=Range("E4:E" & lastrow), order1:=xlDescending, Header:=xlNo

The problem is, it sorts column E but doesn't sort the remaining columns with it. Logan started with a total owed amount of 912.55 and now owes 1524.75.

UqhO1M8.png


What am I not understanding here? thank you!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
That looks like it is because you have formulas in the cells and so when they sort the formula will just recalc the line. If you change them to values then it will probably sort correctly.
The below does for me with values.
Code:
Sub xxx()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A4:K" & lastrow).Sort key1:=Range("E4:E" & lastrow), order1:=xlDescending, header:=xlNo
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

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