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
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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