Excel - excecute macro when following a hyperlink

Anoniem
2023-04-21T11:19:32+00:00

I can use Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) to launch a custom function when a user clicks a cell with a hyperlink. This is working fine.

Now I need the same, but for a cell which has the excel formula HYPERLINK(link_location, [friendly_name]) in it. It seems that Private Sub Worksheet_FollowHyperlink is not called from such a hyperlink.

Am I missing something here or eventually a bug in excel?

Suggestions for a solution to get this working?

Microsoft 365 en Office | Excel | Overige | Windows

Vergrendelde vraag. Deze vraag is gemigreerd vanuit de Microsoft Ondersteuning-community. U kunt met een stem aangeven of de inhoud nuttig is, maar u kunt geen opmerkingen of antwoorden toevoegen of de vraag volgen.

0 opmerkingen Geen opmerkingen
Antwoord geaccepteerd door vraagauteur
Anoniem
2023-04-21T11:58:53+00:00

Hi,

I'm Sneha and I'd be happy to help you out with your question. Sorry for the inconvenience caused.

The Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) event is triggered only when a user clicks on a hyperlink and not when a cell contains the HYPERLINK formula.

One solution to achieve your desired result is to create a custom function that extracts the hyperlink address from the cell containing the HYPERLINK formula and then uses the FollowHyperlink method to open the hyperlink.

I have provided an example code below that you can use as a reference:

Function FollowHyperlinkFromFormula(rng As Range) If InStr(1, rng. Formula, "HYPERLINK(") > 0 Then linkLoc = Mid(rng. Formula, InStr(1, rng. Formula, Chr(34)) + 1, InStr(InStr(1, rng. Formula, Chr(34)) + 1, rng. Formula, Chr(34)) - InStr(1, rng. Formula, Chr(34)) - 1) ActiveWorkbook.FollowHyperlink linkLoc End If End Function

This function takes a range as input and checks if the range contains a HYPERLINK formula. If the range contains a HYPERLINK formula, the function extracts the hyperlink address and opens it using the FollowHyperlink method.

To use this function, you can call it from a cell by entering the formula =FollowHyperlinkFromFormula(A1) where A1 is the cell containing the HYPERLINK formula.

Please note that this approach may not work if the HYPERLINK formula is nested within other formulas or if the friendly name contains a quotation mark. In such cases, you may need to modify the function to handle these situations.

If you have any other questions or need assistance with anything, please don't hesitate to let me know. It will be my pleasure to Assist you.

Best Regards, Sneha

Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

Was dit antwoord nuttig?

1 persoon vond dit antwoord nuttig.
0 opmerkingen Geen opmerkingen

0 extra antwoorden

Sorteren op: Meest nuttig