PDA

View Full Version : VBA Web Query



jtrib84
03-28-2012, 11:55 AM
The code below is my start to retreiving data from the US Bureau of Labor Statistics website.

From Excel, a user will type in the various parameters in specific cells:
1- "Series ID's"
i.e. cwur0000sam, cwur0000sa0, cuur0000sa0, cwur0000sa0L5
2- Year Range: "From Year" & "To Year"

The following are other formatting parameters that need to remain contstant and will not change
3- View of Data: "Column Format"
4- Output Type: "Text Comma Delimited"

So far, I have step "1" 90% complete. If i enter the series ids manually and seperate each by a comma or on a new line, then my search returns the results for each ID.

Since the number of series id's will change, I defined them as an array in VBA and joined them with a comma. This is where i run in to trouble. The search will try and return results for "cwur0000sam,cwur0000sa0,cuur0000sa0,cwur0000sa0L5,ceu1021210008" instead of each one seperately.

any ideas? i'm also going to need some help with the remainder of my code.

Sub PullBLS()

Dim ie As Variant
Set ie = CreateObject("InternetExplorer.Application")
Dim itm As Variant

With ie
.Visible = True
.navigate "http://data.bls.gov/cgi-bin/srgate (http://data.bls.gov/cgi-bin/srgate)"
Do While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Loop
End With

'series ID's can vary depending on user input
'They are located on Sheet3, Range A2:A?
Sheets("Sheet3").Select
With ActiveSheet
arr = WorksheetFunction.Transpose(.Range(.[A2], .Cells(Rows.Count, "A").End(xlUp)))
If Not IsArray(arr) Then arr = Array(arr)
Set itm = ie.document.getElementById("Series_id")
If Not itm Is Nothing Then itm.Value = Join(arr, ",")
End With

With ie
.document.forms(2).submit
Do While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Loop
End With

With ie
.document.forms(2).submit
Do While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Loop
End With

End Sub