Reputation: 13
I am trying to create hyperlinks based on the cells content in a user selected range of cells. I have gotten this far however when it runs it cycles through loop but does not create any hyperlinks.
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
Upvotes: 1
Views: 7573
Reputation: 17041
Edited Nothing more than two typos and missing CStr()
calls! Hyperlink
should be Hyperlinks
, and Adress
should be Address
. The code you have compiles fine because Range.Item
returns a Variant
, not a Range
, so Excel can't flag such errors at compile time. The following works on my Excel 2013 installation:
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
Upvotes: 1
Reputation: 3745
you must be change :
Adress
by Address
and
Hyperlink
by Hyperlinks
Upvotes: 0