简体   繁体   English

Excel VBA用于在单元格中呈现超链接

[英]Excel VBA for rendering a hyperlink in a cell

I would like to write my own macro / function in VBA for Excel that introduces a new "formula" JIRA(ISSUE_ID) in Excel so that I can use 我想在VBA for Excel中编写自己的宏/函数,以在Excel中引入新的“公式” JIRA(ISSUE_ID) ,以便可以使用

=JIRA("ISSUE_ID")

in a cell and it renders the following link (pseudo Markdown syntax) 在单元格中,它呈现以下链接(伪Markdown语法)

[ISSUE_ID](http://my.jira.com/browse/ISSUE_ID)

in the very same cell, where [ISSUE_ID] is the link text to be shown in the cell and (http://my.jira.com/tracker/ISSUE) is the URL for the link. 在同一单元格中,其中[ISSUE_ID]是要在单元格中显示的链接文本,而(http://my.jira.com/tracker/ISSUE)是链接的URL。

Here is an example that hopefully clarifies my needs: 这是一个示例,希望可以阐明我的需求:

I use the "formula" =JIRA("ABC-1234") and what my VBA function should do, is rendering a hyperlink into the very same cell that holds this formula which shows ABC-1234 as the content of the cell which is a hyperlink to http://my.jira.com/browse/ABC-1234 . 我使用“公式” =JIRA("ABC-1234")而我的VBA函数应该如何将超链接呈现到保存此公式的同一单元格中,该公式将ABC-1234显示为该单元格的内容超链接至http://my.jira.com/browse/ABC-1234

In VBA pseudo-code, my function writes like this: 在VBA伪代码中,我的函数是这样写的:

Function JIRA(issue_id)
    current_cell = cell_in_which_this_function_is_used_as_formula()
    url = "http://my.jira.com/browse/" + issue_id
    current_cell.content = issue_id     'text to be shown in the cell
    current_cell.hyperlink = url        'hyperlink to be used for the cell
End Function

I can achieve the same result with =HYPERLINK("http://my.jira.com/browse/ISSUE", "ISSUE") but I don't want to write this lengthy function every time. 我可以使用=HYPERLINK("http://my.jira.com/browse/ISSUE", "ISSUE")达到相同的结果,但是我不想每次都编写这个冗长的函数。 I also don't want to use 2 columns to achieve this (eg =Hyperlink("http://my.jira.com/" & B1,B1) ). 我也不想使用2列来实现这一点(例如=Hyperlink("http://my.jira.com/" & B1,B1) )。

I'm not sure this is possible. 我不确定这是否可能。 You could just write a subroutine to the worksheet change event to automatically add =HYPERLINK("http://my.jira.com/TRACKER/ISSUE", "ISSUE") where you need it whenever cells are updated in the columns holding TRACKER and ISSUE. 您只需在工作表更改事件中编写一个子例程即可自动将=HYPERLINK("http://my.jira.com/TRACKER/ISSUE", "ISSUE")添加到包含TRACKER列的单元格中时需要的子例程和问题。 You could simply build the formula off of the text entered into the cells. 您可以简单地根据输入到单元格中的文本来构建公式。

Or, you could do this: 或者,您可以这样做:

=Hyperlink("http://my.jira.com/" & A1 & "/" & B1,B1)

Assuming that your Tracker column is in column A and your Issue column is in column B. Drag and drop the formula and it will self adjust. 假设“跟踪器”列在A列中,而“问题”列在B列中。拖放公式,它将自动调整。

Actually, there is a way. 实际上,有一种方法。 In ThisWorkbook: 在本工作簿中:

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    On Error Resume Next

    If Target.Cells.Count = 1 Then
        Dim cell As Range
        Set cell = Target.Cells(1, 1)
        If LCase(Left(cell.formula, 5)) = "=jira" Then
            If cell.Hyperlinks.Count > 0 Then
                cell.Hyperlinks.Delete
            End If
            Dim issue As String
            issue = Evaluate(cell.formula)
            cell.Hyperlinks.Add cell, _
                                "http://my.jira.com/browse/" & issue, _
                                issue, _
                                "Click to view issue " & issue
        End If
    End If

End Sub

and in a module 并在一个模块中

Public Function Jira(id As String)
    Jira = id
End Function

JIRA在行动

Here you can just put the value Issue001 (or whatever the issue is) inside the cell and run this code 在这里,您可以将值Issue001 (或其他问题)放入单元格中并运行此代码

Sub setTheHyperLink()
    Dim lastPart
    Dim theScreenTip
    Dim i
    Dim rngList As Range
    Dim theLink

    Set rngList = Selection 'set the range where you have the "Issue"

For Each i In rngList
    lastPart = i.Value
    theScreenTip = lastPart
    theLink = "http://my.jira.com/TRACKER/" & theScreenTip

    If i.Hyperlinks.Count > 0 Then
        i.Hyperlinks(1).Address = theLink
        i.Hyperlinks(1).ScreenTip = theScreenTip
        i.Hyperlinks(1).TextToDisplay = theScreenTip
    Else
        i.Hyperlinks.Add _
        Anchor:=i, _
        Address:=theLink, _
        ScreenTip:=theScreenTip, _
        TextToDisplay:=theScreenTip
    End If
Next i

With that cell defined, you don't need the UDF with this. 定义了该单元格后,您就不需要UDF了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM