sorting column and expanding selection

tanyaleblanc

Board Regular
Joined
Mar 16, 2019
Messages
145
just looking to sort column D in ascending order, but I need all the columns to sort as well. When I use this code, its just sorting column d and the rest of the columns are mixed up and my data is now incorrect.

I do have a header in row 2 that I don't want included in the sort. I just want to sort everything from D3 down and expand selection so everything gets sorted correctly

Range("D3:d500").Sort Key1:=Range("d3"), Order1:=xlAscending, Header:=xlNo
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
the file has columns from A to T, row 2 is the headers, I want to sort by column D (acct#), but all the other columns need to expand to sort those too

for example. I want to sort the below by column D, but all the other data in the other columns need to be sorted with it so that each row has the correct data within it.

[TABLE="width: 957"]
<colgroup><col span="2"><col><col><col><col span="2"><col><col><col></colgroup><tbody>[TR]
[TD]Value Combo [/TD]
[TD]Value Acct#[/TD]
[TD]Cost Centre[/TD]
[TD]Acct #[/TD]
[TD]Name[/TD]
[TD]type[/TD]
[TD]PC[/TD]
[TD]Gross Amt[/TD]
[TD]Comment[/TD]
[TD]SL[/TD]
[/TR]
[TR]
[TD]10494293[/TD]
[TD]1049429[/TD]
[TD="align: right"]1[/TD]
[TD]1049429 [/TD]
[TD]JOAN M JOHNSTON[/TD]
[TD]1[/TD]
[TD]191[/TD]
[TD]$470.20[/TD]
[TD]Payout[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD]10494293[/TD]
[TD]1049429[/TD]
[TD="align: right"]1[/TD]
[TD]1049429 [/TD]
[TD]JOAN M JOHNSTON[/TD]
[TD]1[/TD]
[TD]214[/TD]
[TD]($24.04)[/TD]
[TD]Payout[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD]10876352[/TD]
[TD]1087635[/TD]
[TD="align: right"]1[/TD]
[TD]1087635 [/TD]
[TD]WILMA SMITH[/TD]
[TD]1[/TD]
[TD]191[/TD]
[TD]$330.00[/TD]
[TD]Payout[/TD]
[TD="align: right"]2[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
If it is just A to T then it is just

Code:
Range("A3:T500").Sort Key1:=Range("d3"), Order1:=xlAscending, Header:=xlNo
or
Code:
Range("A2:T500").Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes
Or if an unknown number of columns then
Code:
Range(Cells(2, "A"), Cells(500, Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column)).Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes
 
Upvote 0
If it is just A to T then it is just

Code:
Range("A3:T500").Sort Key1:=Range("d3"), Order1:=xlAscending, Header:=xlNo
or
Code:
Range("A2:T500").Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes
Or if an unknown number of columns then
Code:
Range(Cells(2, "A"), Cells(500, Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column)).Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes

Thank so much for letting me yoink your code! :)
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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