Change single variable to 'for each' argument

Mylarbi

New Member
Joined
Feb 9, 2020
Messages
48
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I have the following VBA code which works.
Currently, it opens a single URL, then clicks a couple of buttons.
I want to move away from having the URL string in the code.
Instead, I want the code to obtain URL from a list of hyperlinks on "Sheet1" column D.
Using a For each argument, each URL is opened and the buttons clicked.
I am a novice in VBA and have not mastered the For each coding, which I need now.
Your help will be appreciated.
VBA Code:
Sub Assign()
    Dim objIE As Object
    Dim URL As String
   
    Set objIE = CreateObject("InternetExplorer.Application")
    objIE.Visible = True

    URL = "https:/educationwebsite.org/course/1047/tutors/find?NameFilter=Ben+Hamilton"

    objIE.Navigate URL
    objIE.document.getElementById("select").Click
    objIE.document.getElementById("add-button").Click
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
    objIE.Quit
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, you could try something like this:

VBA Code:
Sub Assign()
    Dim objIE As Object
    Dim URL As String
    Dim HL As Hyperlink
    Dim WS As Worksheet
   
    Set WS = Sheets("Sheet1")
  
    For Each HL In WS.Hyperlinks
        If Not Intersect(HL.Range, WS.Columns("D")) Is Nothing Then
            Set objIE = CreateObject("InternetExplorer.Application")
            objIE.Visible = True

            URL = HL.Address
    
            objIE.Navigate URL
            objIE.document.getElementById("select").Click
            objIE.document.getElementById("add-button").Click
            Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
            objIE.Quit
        End If
    Next HL

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,195
Members
453,021
Latest member
pingpong7117

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