VBA/Suggestions for a WebBrowser.navigate

privxls

Board Regular
Joined
Nov 22, 2016
Messages
55
Office Version
  1. 2021
Platform
  1. Windows
Hi guys,

Thanks for taking time in viewing this thread. I would like to ask for your suggestions -- maybe you've got some other way to possibly shorten the code / other method for this simple project of mine.

I am working on a simple project where there is a list of websites and once a website is selected from the list, then it would go to the desired website and here's how I somehow did it:

Here is how it looks like:
aqsKu29.jpg



Here's the view to the code & worksheet:
0szMskw.jpg


Code:
Private Sub CommandButton1_Click()




    myLinks = ListBox1.Value
    Select Case myLinks
        Case "360 Safeguard"
            WebBrowser1.Navigate2 "360.cn"
        Case "AliExpress"
            WebBrowser1.Navigate2 "aliexpress.com"
        Case "Amazon"
            WebBrowser1.Navigate2 "amazon.com"
        Case "Apple Inc."
            WebBrowser1.Navigate2 "apple.com"
        Case "Baidu"
            WebBrowser1.Navigate2 "baidu.com"
        Case Blogspot
            WebBrowser1.Navigate2 "blogspot.com"
        Case "Facebook"
            WebBrowser1.Navigate2 "facebook.com"
        Case "Google"
            WebBrowser1.Navigate2 "google.com"
        Case "IMDb"
            WebBrowser1.Navigate2 "imdb.com"
        Case "Imgur"
            WebBrowser1.Navigate2 "imgur.com"
        Case "Instagram"
            WebBrowser1.Navigate2 "instagram.com"
        Case "LinkedIN"
            WebBrowser1.Navigate2 "linkedin.com"
        Case "Microsoft"
            WebBrowser1.Navigate2 "microsoft.com"
        Case "MrExcel"
            WebBrowser1.Navigate2 "mrexcel.com"
        Case "MSN"
            WebBrowser1.Navigate2 "msn.com"
        Case "Netflix"
            WebBrowser1.Navigate2 "netflix.com"
        Case "PayPal"
            WebBrowser1.Navigate2 "paypal.com"
        Case "Reddit"
            WebBrowser1.Navigate2 "reddit.com"
        Case Else
            MsgBox "I'm sorry, that is not yet available", vbInformation, "Ooops!"
    End Select


End Sub

Same goes for another project I am up to as well:
6s5JGOq.jpg

0PSovA7.jpg



Maybe somebody in here would have a way to somehow shorten the codes (I have a list of names where it contains like 50+, and I need to have it in like the directory, imagine the code :O )/ have other ways of doing what I have in mind.

Kind regards,
privxls
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

Oooops, my bad - I am unable to find a way to edit the thread post, but the title should not be: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)


as it should be: VBA/Suggestions - Any Other WAY than This? (for a WebBrowser.navigate)

Thank you!
privxls
 
Last edited:
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

I think it would have been better to ask one question at a time.
You have posted two separate projects you want help on in the same posting.

And you should not be hard coding all this code.

For project #2

You should put all your

Listbox1 values in like column "A"
And other data in columns B,C,D

Then we can refer to the sheet values and not hard coded data in your script.

Here is a script example.

Your Names Being in column "A"

Select a name in the listbox
And then click on the command button

Code:
Private Sub CommandButton2_Click()
On Error GoTo M
Dim SearchString As String
Dim SearchRange As Range
SearchString = ListBox1.Value
Set SearchRange = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
TextBox1.Value = SearchRange.Offset(0, 0).Value
TextBox2.Value = SearchRange.Offset(0, 1).Value
TextBox3.Value = SearchRange.Offset(0, 2).Value
TextBox4.Value = SearchRange.Offset(0, 3).Value
Exit Sub
M:
MsgBox "No such name as  " & Listbox1.Value & "  Found"
End Sub
Private Sub UserForm_Initialize()
ListBox1.List = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
End Sub
 
Last edited:
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

The same sort of script could be written for your first question also.
But I do not understand:
"WebBrowser1.Navigate2 "linkedin.com"

What is: WebBrowser1.Navigate2
 
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

I think it would have been better to ask one question at a time.
You have posted two separate projects you want help on in the same posting.

And you should not be hard coding all this code.

For project #2

You should put all your

Listbox1 values in like column "A"
And other data in columns B,C,D

Then we can refer to the sheet values and not hard coded data in your script.

Here is a script example.

Your Names Being in column "A"

Select a name in the listbox
And then click on the command button

Code:
Private Sub CommandButton2_Click()
On Error GoTo M
Dim SearchString As String
Dim SearchRange As Range
SearchString = ListBox1.Value
Set SearchRange = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
TextBox1.Value = SearchRange.Offset(0, 0).Value
TextBox2.Value = SearchRange.Offset(0, 1).Value
TextBox3.Value = SearchRange.Offset(0, 2).Value
TextBox4.Value = SearchRange.Offset(0, 3).Value
Exit Sub
M:
MsgBox "No such name as  " & Listbox1.Value & "  Found"
End Sub
Private Sub UserForm_Initialize()
ListBox1.List = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
End Sub

Hey, thank you for your response. I have tried this yet I get an error which says "Permission Denied". I will be working on it though, I'll eventually and definitely figure things out in a bit :D

The same sort of script could be written for your first question also.
But I do not understand:
"WebBrowser1.Navigate2 "linkedin.com"

What is: WebBrowser1.Navigate2

Uhmmm.. Is it just for that specific line or for the whole code? I have a WebBrowser in the UserForm. It can be WebBrowser1.Navigate or WebBrowser1.Navigate2 (both works). Not sure if I answered your question, let me know :)

Thank you so much again <3
privxls
 
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

For the first question, try using a 2 column listbox with the 'friendly' name of the site in column 1 and the URL in the second, hidden column.

Then instead of the hard-coded Select Case you just need to extract the URL from the 2nd column of the listbox.
Code:
Dim strURL As String
Dim idx As Long

    idx = ListBox1.ListIndex

    If idx<>-1 Then
        strURL = ListBox1.List(idx,1)
        WebBrowser1.Navigate2 strURL
    End If
 
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

For the first question, try using a 2 column listbox with the 'friendly' name of the site in column 1 and the URL in the second, hidden column.

Then instead of the hard-coded Select Case you just need to extract the URL from the 2nd column of the listbox.
Code:
Dim strURL As String
Dim idx As Long

    idx = ListBox1.ListIndex

    If idx<>-1 Then
        strURL = ListBox1.List(idx,1)
        WebBrowser1.Navigate2 strURL
    End If
Hey I've never thought it'd be that easy using it that way, it would save a lot of time -- best of luck to you my good sir.
Thank you so much, yet I will try to work on the reply of My Answer This and figure things out, the more you experiment, the more you know :D
-privxls
 
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

Permission Denied. I'm not sure why you would get such a message. The script worked for me.
Hey, thank you for your response. I have tried this yet I get an error which says "Permission Denied". I will be working on it though, I'll eventually and definitely figure things out in a bit :D



Uhmmm.. Is it just for that specific line or for the whole code? I have a WebBrowser in the UserForm. It can be WebBrowser1.Navigate or WebBrowser1.Navigate2 (both works). Not sure if I answered your question, let me know :)

Thank you so much again <3
privxls
 
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

Permission Denied. I'm not sure why you would get such a message. The script worked for me.
Don't worry, it's most definitely an error in my end. I'll try to work out on it and I'll eventually get back at you. Thanks so much again my good sir, best of luck!
-privxls
 
Upvote 0
Re: VBA/Suggestions - Any Other This? (for a WebBrowser.navigate)

'Permission Denied' suggests you are using RowSource to populate the listbox.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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