Static Web Query Macro Syntax

Robert McElrath

New Member
Joined
Jul 26, 2008
Messages
14
I have a dynamic web query .iqy file that accesses an exchange rate website, and after entering answers to 4 questions, an exchange rate history table is returned. It works fine.<o:p></o:p>
<o:p> </o:p>
[FONT=&quot]WEB<o:p></o:p>[/FONT]
[FONT=&quot]1<o:p></o:p>[/FONT]
[FONT=&quot]http://www.oanda.com/convert/fxhistory?lang=en&date_fmt=us&exch=USD&expr=EUR&margin_fixed=0&&SUBMIT=Get+Table&format=CSV&redirected=1&date1=["Start","Please enter a starting date."]&date=["End","Please enter an ending date."]&exch2=["From","Please enter the original currency."]&expr2=["To","Please enter the currency to convert to."]<o:p></o:p>[/FONT]
[FONT=&quot]<o:p> </o:p>[/FONT]
[FONT=&quot]Selection=7<o:p></o:p>[/FONT]
[FONT=&quot]Formatting=None<o:p></o:p>[/FONT]
[FONT=&quot]PreFormattedTextToColumns=True<o:p></o:p>[/FONT]
[FONT=&quot]ConsecutiveDelimitersAsOne=True<o:p></o:p>[/FONT]
[FONT=&quot]SingleBlockTextImport=False<o:p></o:p>[/FONT]
[FONT=&quot]DisableDateRecognition=False<o:p></o:p>[/FONT]
[FONT=&quot]DisableRedirections=False<o:p></o:p>[/FONT]
<o:p> </o:p>
However, I would rather store the values of the 4 parameters (date1, date, exch2, expr2) in cells B1, B2, B3, and B4 and have a macro run the web query. I tried with the following, but the ActiveSheet.QueryTables.Add statement has syntax errors that I do not know how to fix. Any suggestions? Many thanks.<o:p></o:p>
<o:p> </o:p>
[FONT=&quot]Sub GetFXHistory()<o:p></o:p>[/FONT]
[FONT=&quot] With ActiveSheet.QueryTables.Add(Connection:= _<o:p></o:p>[/FONT]
[FONT=&quot] "URL;http://www.oanda.com/convert/fxhistory?lang=en&date_fmt=us&exch=USD&expr=EUR&margin_fixed=0&&SUBMIT=Get+Table&format=CSV&redirected=1"&date1=&range.("B1").value &date=&range("B2").value &exch2=&range("B3").value &expr2=&range("B4").value, Destination:=Range(“A1”))<o:p></o:p>[/FONT]
[FONT=&quot] .Name = "fxhistory_1"<o:p></o:p>[/FONT]
[FONT=&quot] .FieldNames = True<o:p></o:p>[/FONT]
[FONT=&quot] .RowNumbers = False<o:p></o:p>[/FONT]
[FONT=&quot] .FillAdjacentFormulas = False<o:p></o:p>[/FONT]
[FONT=&quot] .PreserveFormatting = True<o:p></o:p>[/FONT]
[FONT=&quot] .RefreshOnFileOpen = False<o:p></o:p>[/FONT]
[FONT=&quot] .BackgroundQuery = True<o:p></o:p>[/FONT]
[FONT=&quot] .RefreshStyle = xlInsertDeleteCells<o:p></o:p>[/FONT]
[FONT=&quot] .SavePassword = False<o:p></o:p>[/FONT]
[FONT=&quot] .SaveData = True<o:p></o:p>[/FONT]
[FONT=&quot] .AdjustColumnWidth = True<o:p></o:p>[/FONT]
[FONT=&quot] .RefreshPeriod = 0<o:p></o:p>[/FONT]
[FONT=&quot] .WebSelectionType = xlSpecifiedTables<o:p></o:p>[/FONT]
[FONT=&quot] .WebFormatting = xlWebFormattingNone<o:p></o:p>[/FONT]
[FONT=&quot] .WebTables = "6"<o:p></o:p>[/FONT]
[FONT=&quot] .WebPreFormattedTextToColumns = True<o:p></o:p>[/FONT]
[FONT=&quot] .WebConsecutiveDelimitersAsOne = True<o:p></o:p>[/FONT]
[FONT=&quot] .WebSingleBlockTextImport = False<o:p></o:p>[/FONT]
[FONT=&quot] .WebDisableDateRecognition = False<o:p></o:p>[/FONT]
[FONT=&quot] .WebDisableRedirections = False<o:p></o:p>[/FONT]
[FONT=&quot] .Refresh BackgroundQuery:=False<o:p></o:p>[/FONT]
[FONT=&quot] End With<o:p></o:p>[/FONT]
[FONT=&quot]End Sub<o:p></o:p>[/FONT]
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Had a similar problem that was fixed by calling and ID'ing your variables from the sheet before they were used in the Query as opposed to within the query.

thus: (Dimension as appropriate)


start_date=sheets("whatever").range("B1")
end_date=sheets("whatever").range("B2")
currency=sheets("whatever").range("B3")
period=sheets("whatever").range("B3")
 
Upvote 0
You need to concatenate the values into the URL.
Code:
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.oanda.com/convert/fxhistory?lang=en&date_fmt=us&exch=USD&expr=EUR&margin_fixed=0&&SUBMIT=Get+Table&format=CSV&redirected=1&date1=" & Range("B1").Value & "&date=" & Range("B2").Value & "&exch2=" & Range("B3").Value & "&expr2=" & Range("B4").Value, Destination:=Range(“A1”))
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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