Running macro via hyperlink

FJMD1003

New Member
Joined
Mar 9, 2011
Messages
16
Hi guys,

I am trying to get a set of hyperlinks on a worksheet to run the macro 'Last_Row' I have written (below):

Code:
[FONT=Calibri][/FONT] 
[FONT=Calibri]Sub Last_Row()
Sheets("Sheet 1").Select
ActiveSheet.Protect Password:="Password", DrawingObjects:=True, _
        contents:=True, Scenarios:=True, _
        userinterfaceonly:=True
    ActiveSheet.EnableAutoFilter = True
If ActiveSheet.AutoFilterMode Then
  If ActiveSheet.FilterMode = True Then
       ActiveSheet.ShowAllData
  End If
End If
    Dim FinalRow As Long
    FinalRow = Range("D" & Rows.Count).End(xlUp).Row
    ActiveSheet.Rows(FinalRow).Select
    Selection.Offset(1, 0).Select
End Sub[/FONT][FONT=Calibri][/FONT]

I have been using the following to try and make it work without success:

Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Run ("Last_Row")
End Sub

The hyperlink is based on an IF formula (i.e. only appears if a certain cell has been checked "Yes". The same hyperlink features across multiple cells in one column ( Column V).

Can anyone help? Getting desperate ;)

Cheers,

Fred
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
is the hyperlink for more than just running the macro (ie actually a hyperlink to a website/etc)? If so, why not just use a button with its visibility matched to the "yes" checkbox?
 
Upvote 0
Hi Klarowe,

It's for nothing more than running the Macro, so your suggestion sounds good - how do you modify the visibility settings on a button?

CHeers,

Fred
 
Upvote 0
One more thing, is it just on a single sheet or will it appear on multiple sheets?
if just a single sheet then:

On the sheet where you want to put the button, insert a command button (not a form button). Double click the button and insert this code:
Code:
Run ("Last_Row")

Finally, whereever you want the checkbox, insert the checkbox with this code:
Code:
Sheet1.CommandButton1.Visible = Sheet1.CheckBox1.Value

Make sure to change the macro name, main code, "sheet1", commandbox#, and checkbox# to match your workbook.

There may be a better way of doing this, but it should get you what you need.

Editted since I completely forgot you had posted your original code.. lol.
 
Last edited:
Upvote 0
If you need multiple buttons and checkboxes, then the only way I know to do it would be to insert multiple buttons and checkboxes with the same codes. I'm sure someone can chime in if there is a better way... I'm still a noob when it comes to this stuff... lol
 
Upvote 0
Thanks very much for the input Klarowe, but I think this will still work best with the hyperlink option as it's tidier and simpler to follow for the specific audience - has anyone else got any input?

Any ideas very gratefully received....

Fred
 
Upvote 0
Again, I'm sure there is a better way of specifying the target range selection, but here is one way to write it so it gives you a "link"
Type this in the worksheet page that you want the checkbox/link to be and change the sheet name and cell as needed.
Code:
Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
        With Sheet1.Range("C4")
            .Value = "Link"
            .Font.ColorIndex = 5
            .Font.Bold = True
        End With
    Else
        Sheet1.Range("C4").Value = ""
    End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo Er
    If Intersect(Target, Range("C4")) = "Link" Then
        Run ("Last_Row")
    Else: Exit Sub
    End If
Er: Exit Sub
End Sub
 
Upvote 0
Missed the edit window, but here is a little better code:
Code:
Private Sub CheckBox1_Click()
    If CheckBox1.Value = True Then
        With Sheet1.Range("C4")
            .Value = "Link"
            .Font.ColorIndex = 5
            .Font.Bold = True
            .Font.Underline = xlUnderlineStyleSingle
        End With
    Else
        Sheet1.Range("C4").Value = ""
    End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$C$4" Then
        If Target.Value = "Link" Then Run ("Last_Row")
    Else: 'do nothing
    End If
End Sub

Editted to fix code.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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