Different way to sort?

M

Melvin Purvis

I thought I had everything working properly in this weekends project. Tonight,
I imported the weekly csv, and now I have another problem:

When I'm finished with everything, the last step is to sort it all based on 3
columns.

I recorded a macro to do that, shown below. I see the problem now is when I
record it, it throws in values like "Y11290". I don't want it to be Y11290,
because if next weeks file is 15,000 lines long, it won't all get sorted. How
do I change this so that it always sorts on all of the data?

Thanks in advance.

Columns("A:AD").Select
Range("AD1").Activate
ActiveWorkbook.Worksheets("import").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("import").Sort.SortFields.Add Key:=Range( _
"AD2:AD11290"), SortOn:=xlSortOnValues, Order:=xlAscending,
DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("import").Sort.SortFields.Add Key:=Range( _
"F2:F11290"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("import").Sort.SortFields.Add Key:=Range( _
"Y2:Y11290"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("import").Sort
.SetRange Range("A1:AD9325")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
 
B

Bob Phillips

Untested

Dim LastRow As Long

Columns("A:AD").Select
Range("AD1").Activate
LastRow = Cells(Rows.Count, "AD").End(xlUp).Row
With ActiveWorkbook.Worksheets("import")

.Sort.SortFields.Clear
.Sort.SortFields.Add _
Key:=Range("AD2").Resize(LastRow - 1), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Sort.SortFields.Add _
Key:=Range("F2").Resize(LastRow - 1), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
.Sort.SortFields.Add _
Key:=Range("Y2").Resize(LastRow - 1), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortNormal
With .Sort
.SetRange Range("A1").Resize(LastRow - 1)
.Header = xlYes
.SortMethod = xlPinYin
.Apply
End With
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top