WinHttpRequest as a function (us census API)

aabrahamson78

New Member
Joined
Feb 21, 2016
Messages
3
Hi All,

I have made a Sub that I would like to turn into a function, but am having issues getting it to work. here is the Sub I got to work and the function I am trying to get to work

Code:
Public Sub Runapi()



    Dim W As Worksheet: Set W = ActiveSheet
    
    Dim URL As String: URL = "http://api.census.gov/data/2014/acs5?get=B19037_001E&for=state:01&key=8f7a0306e78e39c8c50480814428cff2de878daf"
    Dim X As New WinHttpRequest
    X.Open "GET", URL, False
    X.send
    Dim Resp As String: Resp = X.ResponseText
    Dim Lines As Variant: Lines = Split(Resp, ",")
    Dim sLines As String
    Dim Values As Variant
    sLines = Lines(2)
    Dim fLines As Variant
    fLines = Split(sLines, "[")
    rlines = fLines(1)
    
    rlines = fLines(1)
    Dim Runapi As String


Runapi = rlines
Debug.Print Runapi




End Sub
here is the function I am trying to get as I would like to be able to input whatever state and whatever code from selected cells in the sheet.

HTML:
Public Function Runapi(State As String, code As String)

    Dim W As Worksheet: Set W = ActiveSheet        Dim URL As String: URL = "http://api.census.gov/data/2014/acs5?get=" & code & "&for=state:" & State & "&key=8f7a0306e78e39c8c50480814428cff2de878daf"    Dim X As New WinHttpRequest    X.Open "GET", URL, False    X.send    Dim Resp As String: Resp = X.ResponseText    Dim Lines As Variant: Lines = Split(Resp, ",")    Dim sLines As String    Dim Values As Variant    sLines = Lines(2)    Dim fLines As Variant    fLines = Split(sLines, "[")    rlines = fLines(1)Runapi = rlines
End Function
 

aabrahamson78

New Member
Joined
Feb 21, 2016
Messages
3
Code:
Public Function Runapi(State As String, code As String)



    Dim W As Worksheet: Set W = ActiveSheet
    
    Dim URL As String: URL = "http://api.census.gov/data/2014/acs5?get=" & code & "&for=state:" & State & "&key=8f7a0306e78e39c8c50480814428cff2de878daf"
    Dim X As New WinHttpRequest
    X.Open "GET", URL, False
    X.send
    Dim Resp As String: Resp = X.ResponseText
    Dim Lines As Variant: Lines = Split(Resp, ",")
    Dim sLines As String
    Dim Values As Variant
    sLines = Lines(2)
    Dim fLines As Variant
    fLines = Split(sLines, "[")
    rlines = fLines(1)
Runapi = rlines


End Function
 

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,999
The function seems to work for me.

Put 01 in A1 and B19037_001E in B1.

Put =Runapi(A1,B1) in C1

C1 shows "1842174".

I don't know if that is the expected result, but no errors occur.
 

aabrahamson78

New Member
Joined
Feb 21, 2016
Messages
3
Your right it is working I guess I just need a new fresh excel and a second pair of eyes
thank you for checking me I was starting to go mad
 

Forum statistics

Threads
1,082,104
Messages
5,363,164
Members
400,720
Latest member
Pettel

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top