Sort row based on one column

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
Hi, I need to filter a range based on the date in column A. I am using the below code for that, but it is not working 100%. In short, I want to filter either ascending or descending, and the complete row need to be moved in the correct order ( Col A-H )

Does anybody have an idea on how to improve the code?

VBA Code:
Sub Sort()

Range("A1").Select

Range(Selection, Selection.End(xlToRight)).Select

Range(Selection, Selection.End(xlDown)).Select

Selection.Sort key1:=Range("A1", Range("A1").End(xlDown)), _

order1:=xlAscending, Header:=xlNo

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Have you tried using the macro recorder while doing it manually?

Do you want to Filter, Sort or do both, and do you want to do this top to bottom or left to right? Your post could be interpreted in one of many ways, a little clarity wouldn't go amiss.
 
Upvote 0
Maybe
VBA Code:
Sub Brutusar()
   Range("A1").CurrentRegion.Sort Range("A1"), xlAscending, , , , , , xlNo
End Sub
 
Upvote 0
Have you tried using the macro recorder while doing it manually?

Do you want to Filter, Sort or do both, and do you want to do this top to bottom or left to right? Your post could be interpreted in one of many ways, a little clarity wouldn't go amiss.


Hi, is some clarification ( I hope)

- column A will always have dates (dd/mm/yyyy)
- column D-H will always have text

- when data is pasted into the Excel sheet it is always unordered by data
- I need to order the rows in either ascending or defending dependent of the situation where the code is used
- when the dates in column A is being sorted, all the other columns in the rows need to be moved in the same order as the date in column A. So if for there are 10 rows, and the earliest date is in for example A5, the range A5 to H5 need to be moved to A1-H5. Same principle for the other rows.
 
Upvote 0
Maybe
VBA Code:
Sub Brutusar()
   Range("A1").CurrentRegion.Sort Range("A1"), xlAscending, , , , , , xlNo
End Sub


That simple, and works perfectly...... Thank you very much!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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