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

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
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,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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