Sorts all but one record

erutherford

Active Member
Joined
Dec 19, 2016
Messages
449
Should be pretty simple stuff, but I have run out of possibilities.

The code sorts data in Column I, first in an ascending order, then allows me to print the results, then resorts in descending order by Column A, putting it back to its original order.
Here is the code
<code>
Private Sub CommandButton3_Click() 'Print by Divison
Range("A3:I52").Sort Key1:=Range("I1"), Order1:=xlAscending, Header:=xlYes
Range("A3:I52").PrintPreview
Range("A3:I52").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
End Sub
</code>

The problem: its sorts everything but the first record (A3).

Column A are numbers, all have the same properties
Column I are text, all have the same properties

As you can see column "I" is not a 1 in the code.

thoughts?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Your code says that row 3 is a header row and not to be sorted. If you want the first row to be sorted:
Code:
Range("A3:I52").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=[COLOR="#FF0000"]xlNo[/COLOR]
 
Last edited:
Upvote 0
It is because of your last argument:
Code:
[COLOR=#333333][FONT=monospace]Header:=xlYes[/FONT][/COLOR]
That tells Excel that the first row in the range you elected to sort (A3:I52) has a header/title row (so exclude that from the sort).

Change that to this and it will work the way you want:
Code:
[COLOR=#333333][FONT=monospace]Header:=xlNo[/FONT][/COLOR]
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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