VBA - Hyperlink to sort data

Zakkaroo

Active Member
Joined
Jul 6, 2009
Messages
383
Good morning

I have a sheet called KPIs with a table of data from B4:M72

What I'd like to do is make all of the headings (B4:M4) into Hyperlinks - so that when you click that hyperlink, it sorts the data by that particular column.

I assume I'll need to use Worksheet_FollowHyperlink - but I'm not sure what the code would be.

Any help, much appreciated!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Hi bewsh, thanks for the quick response.
Based on the link you've provided, I assume what you're getting at is that the code would look like ...



Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

If Target.Range.Address = "$B$4" Then

Elseif Target.Range.Address = "$C$4" Then

Elseif Target.Range.Address = "$D$4" Then

Elseif Target.Range.Address = "$E$4" Then

Elseif Target.Range.Address = "$F$4" Then

etc. etc. all the way to $M$4

?

I have to admit, I was sort of hoping there might be something that could be done with variables to define which column I'd clicked on and sort by that column, rather than specify every eventuality :) :)
 
Last edited:
Upvote 0
you could use the below

ensuring that you have all the title with macro names regardless of hyperlink

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     
 Dim my_sub As String
     
     
    If Not Application.Intersect(Target, Range("B4:M4")) Is Nothing Then
    
   my_sub = Target.Value
    
        Run my_sub
        
        
        
        
    End If
 
Upvote 0
Hi bewsh

Thank you again - I've had a minor revelation and ended up with this:

Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim SortBy As String

SortBy = Target.Range.Value

    ActiveWorkbook.Worksheets("KPIs").ListObjects("Table2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("KPIs").ListObjects("Table2").Sort.SortFields.Add _
        Key:=Range("Table2[" & SortBy & "]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("KPIs").ListObjects("Table2").Sort.SortFields.Add _
        Key:=Range("Table2[Area]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("KPIs").ListObjects("Table2").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
End Sub

Which seems to be working for me! Thanks again though for replying.
 
Upvote 0

Forum statistics

Threads
1,212,933
Messages
6,110,752
Members
448,295
Latest member
Uzair Tahir Khan

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