从Excel VBA中的单元格内容创build超链接

我正在尝试基于用户select的单元格区域中的单元格内容创build超链接。 我已经得到了这一点,但是当它运行循环通过循环,但不创build任何超链接。

Sub AcctHyperlink() Dim WorkRng As Range On Error Resume Next Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("Range", "Select Range", WorkRng.Address, Type:=8) For i = WorkRng.Rows.Count To 1 Step -1 If WorkRng.Cells(i, 1).Value <> "" Then WorkRng.Cells(i, 1).Hyperlink.Add Anchor:=WorkRng.Cells(i, 1), _ Adress:="https://example.com/" & WorkRng.Cells(i, 1).Value & "/search", _ TextToDisplay:=WorkRng.Cells(i, 1).Value End If Next End Sub 

编辑没有超过两个错别字和缺lessCStr()调用! Hyperlink应该是Hyperlinks ,并且Adress应该是Address 。 你编译好的代码,因为Range.Item返回一个Variant ,而不是一个Range ,所以Excel不能在编译时标记这样的错误。 以下工作在我的Excel 2013安装上:

 Option Explicit '<--- always use this for more robust code Sub AcctHyperlink() Dim WorkRng As Range 'On Error Resume Next '<--- Omit for error checking Set WorkRng = Application.Selection Set WorkRng = Application.InputBox("Range", "Select Range", WorkRng.Address, Type:=8) Dim i as Long '<--- Need this because of Option Explicit Dim addr as String '<--- ditto For i = WorkRng.Rows.Count To 1 Step -1 If WorkRng.Cells(i, 1).Value <> "" Then addr = "https://insight.metavante.org/opstopb1/OpstopServlet/Search?activityID=ViewProfileLnNote&activityType=note&activityTrgtID=undefined&activityAction=search&profileView=&accountNumber=" & CStr(WorkRng.Cells(i, 1).Value) & "&accountType=&subAccountNumber=&prcsGrpID=136&RelatedFIs=136&searchBy=account" ' Note: need CStr() ' V--- "Hyperlinks" WorkRng.Cells(i, 1).Hyperlinks.Add Anchor:=WorkRng.Cells(i, 1), _ Address:=addr, _ TextToDisplay:=CStr(WorkRng.Cells(i, 1).Value) '^--- "Address" two lines up ' ^^^^---- Need CStr() End If Next End Sub 

你必须改变:

AddressHyperlinkHyperlinks