How to programmatically perform a web query

ikjohnson

Board Regular
Joined
Sep 19, 2003
Messages
133
From the Microsoft article 213730, comes the following code for a static web query: (I've modified the URL only.)

Code:
Sub URL_Get_Query()

   With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://au.finance.yahoo.com/q?m=a&s=AGL.AX+AMP.AX&d=v1",    Destination:=Range("a1"))

        .BackgroundQuery = True
        .TablesOnlyFromHTML = True
        .Refresh BackgroundQuery:=False
        .SaveData = True
    End With
End Sub

This successfully returns the tables on the found web page back into Excel.
However, the URL will change frequently. I've used the following code to test access to the web with a changing URL:

Code:
Sub URL_Only2()

ActiveWorkbook.FollowHyperlink Address:= _
        [D1].Value, NewWindow:=False, _
        AddHistory:=True

End Sub

where the value of cell D1 is the changing URL.
What I'm trying to do is substitute the [D1].Value into the first lot of code. How could this be done?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Is D1 the entire URL? If so, try:

With ActiveSheet.QueryTables.Add(Connection:= _
Range("D1").Value, Destination:=Range("a1"))

Hope that helps!
 
Upvote 0
Yes, D1 is the entire URL - and it works! I just didn't have the syntax right before.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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