Sort by Active Table, Help.

teksp0rt

New Member
Joined
Feb 6, 2008
Messages
33
Please help. I cannot get this macro to work.... I am trying to sort by the active table, criteria is column 1. Then I would like to add a second critera, column 5 of the table. I keep getting an error on the line...

".Sort.SortFields.Add Key:=Range([activeTable("Column1")]), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal"

HERE IS THE CODE:



Sub SortActiveTable()


Dim ws As Worksheet
Dim tbl As ListObject
Dim activeTable As String


Set ws = ActiveSheet
activeTable = ActiveCell.ListObject.Name


Set tbl = ws.ListObjects(activeTable)

With tbl
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range([activeTable("Column1")]), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.Apply
End With


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
As you put " " around column1, I guess that is the name of the column (title) you want to sort with. Then this macro works

Code:
Sub SortActiveTable()

Dim ws As Worksheet
Dim tbl As ListObject
Dim activeTable As String

Set ws = ActiveSheet
activeTable = ActiveCell.ListObject.Name

Set tbl = ws.ListObjects(activeTable)
With tbl
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range(tbl & "[[#All],[Column1]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.Apply
End With

End Sub

If what you wish is to sort with the first column in position (whatever its name is) then this one would do the job

Code:
Sub SortActiveTable()

Dim ws As Worksheet
Dim tbl As ListObject
Dim activeTable As String

Set ws = ActiveSheet
activeTable = ActiveCell.ListObject.Name

Set tbl = ws.ListObjects(activeTable)

Dim Col1 As String
Col1 = ws.ListObjects(activeTable).ListColumns(1).Name

With tbl
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range(tbl & "[[#All],[" & Col1 & "]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.Apply
End With

End Sub
 
Last edited:
Upvote 0
Please help. I cannot get this macro to work.... I am trying to sort by the active table, criteria is column 1. Then I would like to add a second critera, column 5 of the table.
Code:
    With tbl
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.HeaderRowRange(, 1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.HeaderRowRange(, 5), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.Apply
    End With
 
Upvote 0
Kamolga - Thank you. I used your second example. Thought was cleaner.

Sub SortActiveTable()


Dim ws As Worksheet
Dim tbl As ListObject
Dim activeTable As String
Dim Col1 As String
Dim Col5 As String


Set ws = ActiveSheet
activeTable = ActiveCell.ListObject.Name


Col1 = ws.ListObjects(activeTable).ListColumns(1).Name
Col5 = ws.ListObjects(activeTable).ListColumns(5).Name


Set tbl = ws.ListObjects(activeTable)

With tbl
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range(tbl & "[[#All],[" & Col1 & "]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=Range(tbl & "[[#All],[" & Col5 & "]]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.Apply
End With


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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