Excel Hyperlink to run userform based on row

天涯浪子 提交于 2019-12-12 03:49:19

问题


I have a workbook that all data is managed through userforms, no manual input. I wanted to make it easier to edit a row by making one of the colums called "Edit" with this as its formula :

=HYPERLINK("Edit:>8";"Click to Edit")

The "8" is the row id number automatically added when creating the row

I thought this would work, but aparrently the hyperlink address needs to be valid for this event to trigger :

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    If Left(Target.Address, 8) = "Edit:>" Then
        editRow(Right(Target.Adress,9))
    End If    

End Sub

Any workarounds or better ideas?


回答1:


You need to insert the hyperlink via Ctrl-K shortcut and it works:

and this code

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim rowNum As Long
    rowNum = CLng(Replace(Replace(Target.Address, "https://row", ""), ".com/", ""))

    MsgBox "You are about to edit row " & rowNum

End Sub

returns

But it gets better -- you don't even need to parse the row number from the URL as Hyperlink object provides Range method that you can use to determine the row where it was clicked:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    MsgBox "You are about to edit " & Target.Range.Address

End Sub




回答2:


The HyperLink function doesn't work that way - it's intended not to trigger the FollowHyperlink event.

See:

Worksheet_FollowHyperlink and MsgBox not working in Excel 2010

FollowHyperlink event not working

You could try an inserted hyperlink (probably not very useful) or piggyback on another event like SelectionChange, although that's likely to slow you down because the code will run basically every time you click.




回答3:


In a cell:

=HYPERLINK("#Tester()", "Click to Edit")

In a module:

Function Tester()
    Debug.Print Selection.Row '<< do something with the row
    Set Tester = Selection
End Function

Credit for this approach goes to lori_m: Running VBA from a HYPERLINK()



来源:https://stackoverflow.com/questions/38361689/excel-hyperlink-to-run-userform-based-on-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!