Excel VBA - Open series of links in cells then search website content and return a value into an offset cell if phrase found

ToxicRay

New Member
Joined
Nov 5, 2019
Messages
4
0
I'm looking to carry out a task in VBA of searching a number of websites for a specified phrase.
I've got a spreadsheet with a dynamic data which changes accordingly creating a link in column F. I am looking for a macro to open each individual link, search website content for a specifif phrase and then if the phrase is present on the website then to return a value in a cell to the right Offset by 0,1 otherwise to leave the cell blank and move to next row.
Is such task possible to carry out through vba? I've tried researching similar requests but the result is not what I require.
Code I found online is kind of what I need but it produces the same result if the phrase is present or absent on the website.

Excel Formula:
Option Explicit

Sub SearchForString()
   Dim rngURL As Range
   Dim cll As Range
   Dim stCheck As String
   Dim xmlHttp As Object
   
   On Error Resume Next
   Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
   If xmlHttp Is Nothing Then
      MsgBox "Unable to create XMLHTTP object, it's probably not installed on this machine", vbCritical
      Exit Sub
   End If
   
   Set rngURL = Application.InputBox("Select the range of URLs to check", "Select Range", Selection, Type:=8)
   On Error GoTo 0
   
   If rngURL Is Nothing Then Exit Sub
   
   stCheck = InputBox("Enter the text to search", "", "")
   
   If Len(stCheck) = 0 Then Exit Sub
   
   For Each cll In rngURL.Cells
      If CheckURL(xmlHttp, cll.Value, stCheck) Then
         cll.Offset(, 1).Value = 1
      End If
   Next cll
   
End Sub

Private Function CheckURL(ByRef xmlHttp As Object, ByVal URL As String, ByVal stCheck As String) As Boolean
   Dim stResult As String
   
   If Not LCase$(URL) Like "http://*" Then
      URL = "http://" & URL
   End If
   
   xmlHttp.Open "GET", URL, False
   xmlHttp.Send ""
   
   If xmlHttp.readyState = 4 Then
      If xmlHttp.Status = 200 Then
         stResult = xmlHttp.responseText
         
         If InStr(1, stResult, stCheck, vbBinaryCompare) > 0 Then
            CheckURL = True
         End If
      End If
   End If
End Function

Many thanks in advance.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Is such task possible to carry out through vba?
As it depends on the worksheet design so link a sample workbook on a files host website like Dropbox.​
As a reminder your kind of code searches the text only in the webpage original html code but not in the webpage once full loaded …​
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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