Data call from a website usingVBA

ADBELQMICH

New Member
Joined
Dec 11, 2019
Messages
5
Platform
  1. Windows
Hello everyone, I have to write VBA codes that will allow me to call data from a website, download it in excel format then put it in a current worksheet *database*. I also have to call the data from different URLs at the same time.
The idea is, I will have a button called (update), and when I click on it, it should update the data everytime by reaching out to the URLs and downloading the data then update the worksheet. (I am sorry If Im making this confusing)

These are some URLs that I will be using (example)
Consumer Price Index: Total All Items: Wage Earners for the United States

Could you help me with this please, I am quite new to VBA programming and m stuck on this particular task. I would really appreciate it
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
It looks like you can use the IDs for the different data needed. For example https://fred.stlouisfed.org/graph/fredgraph.xls?id= [the id here]. Then you could probably just open the excel file and copy the data somewhere and do what you need with it.

Really crude example here, but might help you get started or come up with ideas:
Code:
Sub Test()
    Dim URL As String
    Dim id As String
    Dim wb1 As Workbook
    Dim wb2 As Workbook

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set wb1 = ActiveWorkbook
    
    id = "CPALWE01USQ661N"       ' OR   "UNRATE"
    URL = "https://fred.stlouisfed.org/graph/fredgraph.xls?id=" & id

   ' Uses a sheet called Data
    wb1.Sheets("Data").Cells.ClearContents
    Set wb2 = Workbooks.Open(FileName:=URL)
    wb2.ActiveSheet.Cells.Copy
    wb1.Activate
    Range("A1").Select
    ActiveSheet.Paste
    wb2.Close
    
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
thank you so much, it really helped me to start. Just a little question, This will still work even if the data is updated every month (for example), I mean the ID would still remain the same despite the update in the website
 
Upvote 0
thank you so much, it really helped me to start. Just a little question, This will still work even if the data is updated every month (for example), I mean the ID would still remain the same despite the update in the website

It should yes. If you wanted to you could even send other parameters. For example https://fred.stlouisfed.org/graph/f...cosd=2019-01-01&coed=2019-07-01&&fq=Quarterly

If you don't specify other parameters it should just use the defaults though. Same as if you were going to the site and I don't see the IDs changing since their main site uses them:
fred.stlouisfed.org/series/CPALWE01USQ661N
fred.stlouisfed.org/series/UNRATE

Also in my example I'm just using Workbooks.Open(FileName:=URL) to get it, but there at least a few other was of accomplishing it that might look smoother and work quicker.
 
Upvote 0
Can you help me integrate the dynamic selection here please, I tried this but doesn't seem to work.

Sub Test()
Dim URL As String
Dim id As String
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set wb1 = ActiveWorkbook


id = "CPALWE01USQ661N" ' OR "UNRATE"
URL = "https://fred.stlouisfed.org/graph/fredgraph.xls?id=" & id

' Uses a sheet called Data
Set wb2 = Workbooks.Open(Filename:=URL)
Set sht = Worksheets("FRED Graph")
Set StartCell = Range("A12:B12")

LastRow = wb2.sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = wb2.sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

'Select Range
wb2.sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select

wb1.Activate
Range("A1").Select
ActiveSheet.Paste
wb2.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
 
Upvote 0
VBA Code:
Sub Test()
Dim URL As String
Dim id As String
Dim wb1 As Workbook
Dim wb2 As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Set wb1 = ActiveWorkbook


id = "CPALWE01USQ661N" ' OR "UNRATE"
URL = "https://fred.stlouisfed.org/graph/fredgraph.xls?id=" & id

' Uses a sheet called Data
Set wb2 = Workbooks.Open(Filename:=URL)
    Rows("1:10").Delete Shift:=xlUp
    Cells.Copy
wb1.Activate
ActiveSheet.Paste
wb2.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,784
Messages
6,126,878
Members
449,347
Latest member
Macro_learner

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