VBA Button control to open website from DV list

RJSIGKITS

Board Regular
Joined
Apr 15, 2013
Messages
109
Hi Guys.

I need to create a button command that will launch a website that is selected from a DV list.

I currently have a DV list in cell D15, that the user can select a useful web address. I then have a button which when clicked needs to launch the selected website in Google Chrome. Importantly, it must use Chrome as the default web browser, if that makes a difference. Chrome is currently set as my computers default browser.

Think this should be quite easy...

Thanks in advance
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Something along these lines :

Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Sub OpenUrlInDefaultBrowser(ByVal url As String)
    Call ShellExecute(0, "open", url, 0, 0, 1)
End Sub

Usage :
Code:
Sub Test()
    Call OpenUrlInDefaultBrowser(Range("D5"))
End Sub

The code can be improved with some defensive error handling.
 
Upvote 0
Something along these lines :

Code:
Option Explicit

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Sub OpenUrlInDefaultBrowser(ByVal url As String)
    Call ShellExecute(0, "open", url, 0, 0, 1)
End Sub

Usage :
Code:
Sub Test()
    Call OpenUrlInDefaultBrowser(Range("D5"))
End Sub

The code can be improved with some defensive error handling.

Your solution looks rather complicated - after messing around with it all day, I've cracked it with one line of code:
Code:
ActiveWorkbook.FollowHyperlink Address:="http://" & Range("D15").Value, NewWindow:=True
I'm intrigued as to What does your code do that this might not, please?

Thanks so much for your input.
 
Upvote 0
I'm intrigued as to What does your code do that this might not, please?

Nothing really.. I just forgot about the native FollowHyperlink Method.

I would recommend to replace ActiveWorkbook with ThisWorkbook in case the workbook containing the hyperlinks is not the active workbook at the time the code is executed... Also, I would fully qualify Range("D5") with its parent worksheet.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,147
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top