Create urls to cycle through for Web Queries

tmsmyer

New Member
Joined
Sep 17, 2015
Messages
38
I am trying to write a macro that will create several web queries. The urls will be the something like "www.example.com/xxxx"

The xxxx will change depending on cell values. On sheet "numbers" I have a series of numbers that start on A2 and go down an x amount of cells

For instance:
A2=1234
A3=2345
A4=4578
A5=3497

the urls would be: www.example.com/1234, www.example.com/2345, etc.


My question is, how do I get a vba macro to cycle through each possible url and create a web query. The amount of numbers in Column A always change, so I would want the look to end when there is a blank cell.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try this to get started. As well as the sheet named "numbers", it needs a sheet named "queries" on which the web queries are created and data imported.

Code:
Public Sub Web_Queries()

    Dim numberCells As Range, numberCell As Range
    Dim webQueryDestCell As Range
    Dim webQuery As QueryTable
    
    With Worksheets("numbers")
        Set numberCells = .Range("A2", .Cells(Rows.Count, "A").End(xlUp))
    End With
    
    Set webQueryDestCell = Worksheets("queries").Cells(Rows.Count, "A").End(xlUp).Offset(1)
    
    For Each numberCell In numberCells
    
        With webQueryDestCell.Worksheet
            Set webQuery = .QueryTables.Add(Connection:="URL;http://www.example.com/" & numberCell.Value, Destination:=webQueryDestCell)
            With webQuery
                .WebSelectionType = xlEntirePage
                .WebFormatting = xlWebFormattingNone
                .Refresh False
            End With
            Set webQueryDestCell = webQueryDestCell.Offset(webQuery.ResultRange.Rows.Count, 0)
            'webQuery.Delete   'Optional - delete web query
        End With
        
    Next
        
End Sub
The code imports each entire web page into the "queries" sheet. To import the data for your specific URLs, use the macro recorder whilst creating a web query for a specific URL and incorporate the web query part of the code generated within the "With webQuery .... End With" block.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,739
Members
448,989
Latest member
mariah3

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