Loop through a list of URLs in Sheet 1 and strip data from each URL onto a separate worksheet

stirlingmw1

Board Regular
Joined
Jun 17, 2016
Messages
53
Office Version
  1. 2016
  2. 2013
  3. 2010
  4. 2007
Platform
  1. Windows
I have the following code that stripe the entire webpage from a single url and pastes the data onto "Result" worksheet.
VBA Code:
Private Sub UseQueryTable()
    Dim url As String
    Dim table As QueryTable
   
    url = "Page URL"
    Set table = shResult.QueryTables.Add("URL;" & url, shResult.Range("A1"))
        With table
        .WebSelectionType = xlEntirePage
        .WebFormatting = xlWebFormattingNone ' no web formatting.
        .Refresh
    End With
End Sub
I would like to now modify this code so that it loops through a list of URLs I have on Sheet1 Column A (A1:A558") and paste the return to its on worksheet. Sheet1 Column A has a list of URLs and Column B has a list of the desired Worksheet names.
Can anyone help.

TIA

Steve
 

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"
The following code (note that there's no error handling) . . .

VBA Code:
Private Sub CreateQueryTables()

    Dim urlRange As Range
    With Worksheets("Sheet1")
        Set urlRange = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
    
    Dim currentCell As Range
    For Each currentCell In urlRange
    
        If Len(currentCell) > 0 And Len(currentCell.Offset(, 1)) > 0 Then
        
            Dim urlAddress As String
            urlAddress = currentCell.Value
            
            Dim newWorksheetName As String
            newWorksheetName = currentCell.Offset(, 1).Value
            
            Dim newWorksheet As Worksheet
            Set newWorksheet = Worksheets.Add(after:=Sheets(Sheets.Count))
            
            newWorksheet.Name = newWorksheetName
            
            Dim currentQueryTable As QueryTable
            Set currentQueryTable = newWorksheet.QueryTables.Add("URL;" & urlAddress, newWorksheet.Range("A1"))
            
            With currentQueryTable
                .WebSelectionType = xlEntirePage
                .WebFormatting = xlWebFormattingNone ' no web formatting.
                .Refresh
            End With
            
        End If
        
    Next currentCell
    
End Sub

Hope this helps!
 
Upvote 0
Solution
The following code (note that there's no error handling) . . .

VBA Code:
Private Sub CreateQueryTables()

    Dim urlRange As Range
    With Worksheets("Sheet1")
        Set urlRange = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
    End With
   
    Dim currentCell As Range
    For Each currentCell In urlRange
   
        If Len(currentCell) > 0 And Len(currentCell.Offset(, 1)) > 0 Then
       
            Dim urlAddress As String
            urlAddress = currentCell.Value
           
            Dim newWorksheetName As String
            newWorksheetName = currentCell.Offset(, 1).Value
           
            Dim newWorksheet As Worksheet
            Set newWorksheet = Worksheets.Add(after:=Sheets(Sheets.Count))
           
            newWorksheet.Name = newWorksheetName
           
            Dim currentQueryTable As QueryTable
            Set currentQueryTable = newWorksheet.QueryTables.Add("URL;" & urlAddress, newWorksheet.Range("A1"))
           
            With currentQueryTable
                .WebSelectionType = xlEntirePage
                .WebFormatting = xlWebFormattingNone ' no web formatting.
                .Refresh
            End With
           
        End If
       
    Next currentCell
   
End Sub

Hope this helps!
Fantastic Thank you so much, works perfect. I was having trouble with an error but realised that Worksheet tab names are limited to 31 characters, after reducing the data in Column B and adding error handling it worked as I needed.

Thank you again.

Steve
 
Upvote 0
That's great, I'm glad I was able to help.

And I'm glad you were able to deal with that error.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,769
Messages
6,126,787
Members
449,336
Latest member
p17tootie

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