2016-03-07 78 views
1

我想編寫VBA Excel的我自己的宏/功能引入了一個新的「公式」 JIRA(ISSUE_ID)在Excel中,這樣我可以使用Excel的VBA在細胞呈現一個超鏈接

=JIRA("ISSUE_ID")

小區和它呈現下面的鏈接(僞降價語法)

[ISSUE_ID](http://my.jira.com/browse/ISSUE_ID)
在非常相同的細胞

,其中[ISSUE_ID]是在細胞中顯示的鏈接文本和(http://my.jira.com/tracker/ISSUE)是鏈接的URL。

這裏有一個例子是希望澄清我的需求:

我用「公式」 =JIRA("ABC-1234"),什麼我的VBA函數應該做的,是呈現一個超鏈接進入了保存這個公式表示ABC非常相同的細胞-1234作爲與http://my.jira.com/browse/ABC-1234的超鏈接的單元格的內容。

在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 

我可以實現與=HYPERLINK("http://my.jira.com/browse/ISSUE", "ISSUE")相同的結果,但我不想每次都寫這個漫長的功能。我也不想用2列來實現這個目標(例如=Hyperlink("http://my.jira.com/" & B1,B1))。

+1

的可能的複製[添加超鏈接在VBA UDF(http://stackoverflow.com/questions/27585398/add-hyperlink-in-vba-udf) – nbayly

+0

你能請你想要和需要的東西更加清晰。因爲我知道你需要一個和HYPERLINK完全一樣的函數,併發送相同的參數......請放大你的問題。 –

+0

@ElbertVillarreal - 感謝您的評論!我試圖簡化和澄清我的問題。也許現在很清楚我想做什麼。 –

回答

1

我不確定這是可能的。只要在持有TRACKER和ISSUE的列中更新單元格,就可以在工作表更改事件中編寫一個子例程以自動添加=HYPERLINK("http://my.jira.com/TRACKER/ISSUE", "ISSUE")。您可以簡單地從輸入到單元格中的文本中構建公式。

或者,你可以這樣做:

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

假設您追蹤列是列A和您的問題列在B列拖放公式,它會自我調整。

+0

我澄清了我的問題,我希望現在很清楚爲什麼我不接受這個解決方案。 –

+0

然後,您需要將該功能構建到工作表更改事件中。有什麼你想要的解決方法,但沒有簡單的方法來做你想要做的事情,而無需通過工作表更改事件。其他人已經爲此發佈了代碼,所以我不會。 – asp8811

1

其實,有一種方法。在的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 

,並在一個模塊中

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

JIRA in action

0

在這裏你可以把價值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 

定義了該單元格後,您不需要UDF這個。