Trying to Do my first API with VBA

Photomofo

Active Member
Joined
Aug 20, 2012
Messages
259
I'm trying to translate some Python code into VBA. I've tried adapting multiple VBA codes relating to APIs but nothing is going through. Can someone point me in the right direction.

<code style="box-sizing: border-box; font-size: inherit; padding: 0px; color: inherit; background-color: transparent; border-radius: 0px; line-height: 12px;">import requests

url = "http://developer.nrel.gov/api/solar/nsrdb_psm3_download.json?api_key=yourapikeygoeshere"

payload = "names=2012&leap_day=false&interval=60&utc=false&full_name=Honored%2BUser&email=honored.user%40gmail.com&affiliation=NREL&mailing_list=true&reason=Academic&attributes=dhi%2Cdni%2Cwind_speed%2Cair_temperature&wkt=MULTIPOINT(-106.22%2032.9741%2C-106.18%2032.9741%2C-106.1%2032.9741)"

headers = {
'content-type': "application/x-www-form-urlencoded",
'cache-control': "no-cache"
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)</code>
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Here's the code I put together. It works OK but it's running 3 to 5 times slower than I expected. Any obvious reason why?

Code:
 Option Explicit


Sub GET_NSRDB()


Dim Latitude As String
Dim Longitude As String
Dim Year_x As Integer
Dim Attributes As String
Dim i As Long
Dim Latitudes As Variant
Dim Longitudes As Variant


Dim URL As String
Dim URL1 As String
Dim URL2 As String
Dim ARG As String
Dim WinHttpReq As Object
Dim oStream As Object


Longitudes = Sheets("Solar Plants").Range("I1:I2257")
Latitudes = Sheets("Solar Plants").Range("J1:J2257")
Attributes = "attributes=air_temperature,dew_point,dhi,dni,ghi,relative_humidity,surface_albedo,wind_direction,wind_speed&leap_day=false&utc=false&interval=30"


For i = 2 To 2257


    Longitude = Longitudes(i, 1)
    Latitude = Latitudes(i, 1)


    For Year_x = 1995 To 2017
        
        ARG = "C:\Users\XYZ\Desktop\NSRDB\" & Latitude & "_" & Longitude & "_" & Year_x & ".csv"


        If Dir(ARG) <> "" Then
        
            'Skip
        
        Else
        
            URL1 = "https://developer.nrel.gov/api/solar/nsrdb_psm3_download.csv?api_key=XYZ&full_name=XYZ&email=XYZ&"
            URL2 = "wkt=POINT(" & Latitude & "+" & Longitude & ")&names=" & Year_x & "&" & Attributes
            URL = URL1 + URL2


            Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
            WinHttpReq.Open "GET", URL, False, "username", "password"
            WinHttpReq.send
            
            URL = WinHttpReq.responseBody
            If WinHttpReq.Status = 200 Then
                Set oStream = CreateObject("ADODB.Stream")
                oStream.Open
                oStream.Type = 1
                oStream.Write WinHttpReq.responseBody
                oStream.SaveToFile ARG, 2 ' 1 = no overwrite, 2 = overwrite
                oStream.Close
            End If
            
            Application.Wait Now + TimeValue("00:00:01")
        
        End If
        
    Next Year_x
    
Next i


End Sub
 
Upvote 0
Thanks hateme28... I have to insert the 1 second delay because the server only lets you make a call once a second. I've run this program from work and home and in both cases the speed is about the same. I've fixed the code some to eliminate bad calls but this hasn't improved the speed. Basically I think the server is the choke point. This particular data set (the National Solar Radiation Database) is serving a ton of data so I think they've got things set up to budget the load. But again... Thanks...

You guys here on Mr. Excel are awesome. I've come a long way with Excel/VBA thanks to your help.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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