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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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