VBA to extract data from a website and populate on a workbook

keranali

Rules Violation
Joined
Oct 4, 2010
Messages
234
Office Version
  1. 365
Platform
  1. Windows
Good Day all is it possible to create a workbook vba that can extract specific data from a website and automatically populate the cells. then choose a date on the site date picker and search for new data and continue the data population?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Yes, it is possible to create a VBA macro in Excel to extract data from a website and populate cells, and to automate the process of choosing a date and searching for new data. Here are some steps you can follow:

  1. Open the Excel workbook where you want to create the macro.
  2. Press Alt+F11 to open the VBA editor.
  3. In the VBA editor, click on "Insert" and choose "Module" to create a new module.
  4. In the module, create a new sub procedure to automate the data extraction and population. You can use VBA functions like HTTP requests and HTML parsing to extract data from the website, and the Range object to populate the cells in Excel.
  5. Write code to select a date on the website's date picker and search for new data. You can use VBA functions like SendKeys to simulate keyboard input, or use specialized libraries like Selenium or WinAutomation to interact with the website.
  6. Use a loop to continue the data population process for each desired date range. You can set up a counter variable to keep track of the number of iterations, and use an exit condition to stop the loop when all desired data has been extracted.
Here is some sample code to get you started:

VBA Code:
Sub ExtractDataFromWebsite()
    Dim request As Object
    Dim response As String
    Dim html As New HTMLDocument
    Dim datepicker As Object
    Dim date_range As Date
    Dim row_counter As Integer
    
    ' Initialize the date range and row counter
    date_range = #1/1/2022#
    row_counter = 2
    
    ' Create a new HTTP request and send it to the website
    Set request = CreateObject("MSXML2.XMLHTTP")
    request.Open "GET", "http://www.example.com/data?date=" & date_range, False
    request.send
    
    ' Parse the HTML response and extract the desired data
    response = request.responseText
    html.body.innerHTML = response
    Cells(row_counter, 1).Value = html.getElementsByClassName("data1")(0).innerText
    Cells(row_counter, 2).Value = html.getElementsByClassName("data2")(0).innerText
    ' Repeat the above two lines of code for each desired data point
    
    ' Find the date picker element and select a new date range
    Set datepicker = html.getElementById("datepicker")
    datepicker.Value = Format(date_range + 7, "mm/dd/yyyy")
    
    ' Increment the row counter and date range for the next iteration
    row_counter = row_counter + 1
    date_range = date_range + 7
    
    ' Repeat the above steps for each desired date range
End Sub

This code uses a simple HTTP GET request to extract data from a website, and assumes that the data is contained in HTML elements with specific class names (e.g. "data1" and "data2"). You will need to adjust the code to match the specific website and data format you are working with.

Note that automating website interactions with VBA can be tricky and may require some trial and error to get right. You may also need to account for website updates or changes that could break your macro.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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