Excel VBA代码为长链接从单独的单元格显示文本的超链接

我有一个工作表(除了其他不相关的东西外)还有一列中的书名和他们在我们公司书店中的URL。 这些URL及其embedded的跟踪代码通常很长,通常超过255个字符的限制。 这是一个工作文档 – 我经常添加它 – 所以我需要一个只能在我select的行上工作的代码。 我想要做的是:select我目前正在使用的url,并在同一行的新单元格中添加VBA,创build一个显示每个url的书名的超链接。

我拼凑了一块只适用于一个单元的代码。 我遇到的麻烦是使这项工作为我select的一组单元格:

'只适用于一个单元Sub insertVeryLongHyperlink()

Dim curCell As Range Dim longHyperlink As String Dim title As String Set curCell = Range("R2") title = [c2] longHyperlink = [R2] curCell.Hyperlinks.Add Anchor:=Range("t2"), _ Address:=longHyperlink, _ SubAddress:="", _ ScreenTip:=title, _ TextToDisplay:=title 

结束小组

任何帮助,将不胜感激。 如果不是字符限制,这将会容易得多。 非常感谢!

您将需要使用For Each .. Next循环结构。 试试这个(未经testing):

 Sub insertVeryLongHyperlinks() Dim allCells as Range Dim curCell As Range Dim longHyperlink As String Dim title As String Set allCells = Range("R2:R10") '## Modify as needed For each curCell in allCells title = Cells(currCell.Row, "C").Value longHyperlink = curCell.Value curCell.Hyperlinks.Add Anchor:=curCell.Offset(0,1), _ Address:=longHyperlink, _ SubAddress:="", _ ScreenTip:=title, _ TextToDisplay:=title Next End Sub 

关于For Each ... Next文档For Each ... Next和其他的VBA语句可以在这里find:

http://msdn.microsoft.com/en-us/library/office/jj692812(v=office.15).aspx