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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,215,054
Messages
6,122,901
Members
449,097
Latest member
dbomb1414

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