2015-04-18 66 views
3

我的VBA技能不存在,而且我还找不到符合我的情况的任何线程,因此无法使用此线程。使用vba添加超链接到姓名

我在Excel工作表中有一列包含名称(B列),我试图将B中的单元格超链接到网页。每行有一个特定的网页。

如果我有一列包含所有对应的URL,那么使用HYPERLINK函数会很容易,但问题是电子表格的最终版本不会包含带URL的列。

什么最终版本将包括:超链接到特定网页 (B柱)名称, (列A)的ID,其中包括的B

的网址是网址的唯一部分加名除了最后的数字外,所有这些都是相同的。 不改变的部分是:

http://www.regulations.gov/#!documentDetail;D=CFPB-2011-0008 

,并有在最后每个URL一个四位数字。

开始“CFPB”,并与四位数字结尾的一点是,将是包含在列A中的部分

所以我的计划是写一个VBA程序,增加了超链接使用到B网址从

http://www.regulations.gov/#!documentDetail;D= 

构造和相应的细胞在所述前部(例如CFPB-2011-0008-0002)。我正在考虑使用LEFT函数从A中获取URL的第二部分(例如,LEFT(A1,19))。

对不起,如果解释不清楚...任何帮助将不胜感激。

+0

不起作用。由于URL中的'#'。 https://support.microsoft。com/en-us/kb/202261 –

+0

@AxelRichter - 适用于URL的罚款(至少当我在Excel 2010中尝试时)。唯一奇怪的是,工具提示用连字符代替了#,但点击时仍然加载了正确的页面。链接的KB只是指在文件名中使用'#'。 – Comintern

+0

@Comintern。您的答案不适用于Excel 2007.超链接调用失败的“http://www.regulations.gov/%20-%20!documentDetail; D = CFPB-2011-0008”。 –

回答

0

我前几天放了一个脚本来做类似的事情,你会想要把它放到一个循环中或者在电子表格中通过你的列表。我使用iCurrentRow和iCurrentCol来导航我的工作表。

使用您建议的功能在您想要的单元格(即B列中的单元格)中构建超链接字符串,然后将strString设置为该值。我刚刚添加了strString(尚未测试过),所以如果它不起作用,那么您可能需要将它包含在CStr()中。

它应该给你一些工作,无论如何。

' Set the string to the hyperlink address  
strString = Cells(iCurrentRow, iCurrentCol).value 
' Check if the cell already has a hyperlink 
If Cells(iCurrentRow, iCurrentCol).Hyperlinks.Count > 0 Then 
    'If it does then check if it is the same as in the cell 
    If strString <> CStr(Cells(iCurrentRow, iCurrentCol).Hyperlinks(1).Address) Then 
     'Check if there is no new hyperlink 
      If strString = "" Then 
       Cells(iCurrentRow, iCurrentCol).Hyperlinks.Delete 
      Else 
       ActiveSheet.Hyperlinks.Add Anchor:=Cells(iCurrentRow, iCurrentCol), _ 
        Address:=strString 
      End If 
     End If 
Else 
    'If there isn't an existing hyperlink then add it 
    If strString <> "" Then 
     ActiveSheet.Hyperlinks.Add Anchor:=Cells(iCurrentRow, iCurrentCol), _ 
      Address:=strString 
    End If 
End If 
0

试试这个:

Sub MAIN() 
    Dim rng As Range, rr As Range, r As Range 
    Set rng = Intersect(Range("B:B"), ActiveSheet.UsedRange) 

    For Each rr In rng 
     If rr.Value <> "" Then 
      Set r = rr 
      Call hyper_maker(r) 
     End If 
    Next rr 
End Sub 

Sub hyper_maker(r As Range) 
    If r.Hyperlinks.Count > 0 Then 
     r.Hyperlinks.Delete 
    End If 
    txt = r.Value 
    s = "http://www.regulations.gov/#!documentDetail;D=" & txt 
    r.Value = s 
    r.Select 
    Application.SendKeys "{F2}" 
    Application.SendKeys "{ENTER}" 
    DoEvents 
    r.Hyperlinks(1).TextToDisplay = txt 
End Sub 
2

我理解正确的问题,你可以用一个简单的工作表函数做到这一点。正好连接的URL一起:

=HYPERLINK(CONCATENATE("http://www.regulations.gov/#!documentDetail;D=",LEFT(A1,14))) 

一个VBA解决方案,只是网址添加到现有的文件名会是这样的:

Sub AddHyperlinks() 

    Dim url As String 

    Dim current As Range 
    For Each current In Selection.Cells 
     url = "http://www.regulations.gov/#!documentDetail;D=" & _ 
       Left$(current.Value, 14) 
     current.Worksheet.Hyperlinks.Add current, url 
    Next current 

End Sub 

选择要添加超链接到细胞和运行宏。

+0

我刚刚运行它,它的工作原理完全不同,只不过它看起来像显示的URL以A列中名称的前14个字符而不是B列中的ID结尾。我想在引用时超链接名称ID。假设带有ID的列位于名称列的左侧,是否有办法将Left函数中的current.Value替换为表示“活动单元格左侧的单元格”?非常感谢你。 – hwin

+1

@hwin - 你可以用'current.Offset(0,-1).Value'替换'current.Value' – Comintern