Filtering a table by clicking on column header

mark hansen

Well-known Member
Joined
Mar 6, 2006
Messages
534
Office Version
  1. 2016
Platform
  1. Windows
I have a data set that was just a plain data set (not a table). I set up a macro in the Worksheet_SelectionChange event that when I clicked on the column header it would sort on that column. Now the data set is a Table because I need to replicate formulas to get additional information from the data. I recorded the sort in the table and compaired it with the code I set up, and the only difference is the Key:= parameter.

In my (non table) code the sort line is
Code:
  Range(Cells(20, ActiveCell.Column), Cells(100000, ActiveCell.Column)), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal


You can see I use the Activecell.column to tell the code what column to use.

the recorded Table code is
Code:
ActiveWorkbook.Worksheets("Data").ListObjects("Table1").Sort.SortFields.Add _
        Key:=Range("Table1[[#All],[CM]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortNormal

How can I change the Key:= parameter to have a variable column?

Thanks
Mark
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,712
Members
449,093
Latest member
Mnur

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