Basic sort macro

213monkey

New Member
Joined
Oct 30, 2014
Messages
1
Hello---- I am pretty new with VBA/macros, and I am having a very difficult time creating a sort macro.
We have an SQL based database management application and when we run reports it spits out many unused and/or unused columns.
I have been creating .xlsm templates to cut down some of the work arranging/deleting the columns after exporting from SQL.
Since I am copying and pasting into this sheet the amount of rows change every time so I need to change the sort macro to account for that. This is what it looks like now. I understand that it is showing a range that was selected when I was recording the macro, but I would like the range to be dynamic. any help would be greatly appreciated!

Cells.Select ActiveWorkbook.Worksheets("test").SORT.SortFields.Clear
ActiveWorkbook.Worksheets("test").SORT.SortFields.Add Key:=Range("I2:I53"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("test").SORT.SortFields.Add Key:=Range("B2:B53"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("test").SORT.SortFields.Add Key:=Range("G2:G53"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("test").SORT.SortFields.Add Key:=Range("J2:J53"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("test").SORT
.SetRange Range("A1:O53")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Welcome to the MrExcel board!

From reading your description I'm thinking you are wanting to sort all the data on the sheet.
If so, try the code below on a copy of your workbook. If there is data in other columns to be excluded, then post back and I;ll modify.

Rich (BB code):
Sub mySort()
  With ActiveWorkbook.Worksheets("test").Sort
    With .SortFields
      .Clear
      .Add Key:=Range("I2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
      .Add Key:=Range("B2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
      .Add Key:=Range("G2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
      .Add Key:=Range("J2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    End With
    .SetRange .Parent.UsedRange
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,693
Messages
6,192,468
Members
453,726
Latest member
JoeH57

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