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
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