VBA: Data from URL on worksheet

thomasje1234

New Member
Joined
May 26, 2015
Messages
2
Hi guys,

I'm trying to get data from a URL, which is working. But I would like to be able to change the URL without having to change the VBA code. So for example: If I paste the URL in cell B3, how can I get the VBA code to get the data from the URL in that cell?

The part of the code that works looks like this:

Sheets("Sheet1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.bom.gov.au/jsp/ncc/cdio/weatherData/av?p_nccObsCode=139&p_display_type=dataFile&p_startYear=&p_c=-326660605&p_stn_num=040412" _
, Destination:=Range("$A$1"))
.Name = _
"av?p_nccObsCode=139&p_display_type=dataFile&p_startYear=&p_c=-326660605&p_stn_num=040412"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub



So the question is: How can I change that piece of code, so that it gets the data from the URL in a cell on sheet1?

It sounds like it shouldn't be too hard, but I can't find a solution. Thanks for reading this :)

Kind regards, Thomas
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi Thomas,

Welcome aboard!

Try the following with the URL in B3:

Code:
Public Sub Test()

Sheets("Sheet1").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;"[COLOR=#ff0000] & ActiveSheet.Range("B3").Text & ""[/COLOR], Destination:=Range("$A$1"))
.Name = _
"av?p_nccObsCode=139&p_display_type=dataFile&p_startYear=&p_c=-326660605&p_stn_num=040412"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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