Please, try it in the next way:
Sub fillDropDown()
Dim cB As DropDown, arr, El
Set cB = ActiveSheet.Shapes("DropDown7").OLEFormat.Object
arr = Split("https://google.com,https://example.microsoft.com,a wrong link", ",")
cB.RemoveAllItems
For Each El In arr
cB.AddItem El
Next
End Sub
Copy the above code in a standard module, or in a sheet code module, and run it. If the sheet will be the one keeping the combo, you can use Me
instead of ActiveSheet
.
It will load in the combo (dropDown) the array elements.
You can build the array in the next way, too:
arr = Array("https://google.com", "https://example.microsoft.com", "a wrong link")
Then, use the next code to access the hyperlink:
Sub runHyperlink()
Dim cB As DropDown, arr, El
Set cB = ActiveSheet.Shapes("DropDown7").OLEFormat.Object
If cB.Value <> 0 Then
On Error Resume Next
ActiveWorkbook.FollowHyperlink Address:=cB.list(cB.Value)
If Err.Number = -2147221014 Then
Err.Clear: MsgBox "The used link is not valid..."
End If
On Error GoTo 0
Else
MsgBox "You should select an option in the combo..."
End If
End Sub
You can assign the above code to a Form
, shape
button or place it in a Click
event of an ActiveX
button. The code or call the sub, simple writing: runHyperlink
in the event code.