In a previous post we learnt to import data from Google Sheets using a web query in Excel (Import Data From Google Sheets). We can actually create a web query in Excel either manually or with a simple VBA macro. In this post we see another method to import data from Google Sheets that allows getting specific rows, columns, and cells or values depending on certain criteria. In this other method, we send a HTTP request to the Google spreadsheet URL to get the data, and then we loop through the HTML response table to extract the values of interest according to a chosen criterion.
Macro/VBA code:
Macro explained:
- First of all, we need to add the Microsoft XML v6.0 and Microsoft HTML Object libraries to the VBA project under Tools -> References in the Visual Basic editor.
- We declare HTTPreq as an object variable to later create and assign a ServerXML object to it (late binding). We do the same with the HTML object variable. We also declare some other variables to store the Google spreadsheet ID or key, the Google Sheets web page URL, and the row and column index variables used while looping through the response.
- The Google spreadsheet ID or key can be found after “/d/” and before the following forward slash in the Google Sheets sharing link (see below).
- Note that the Google spreadsheet needs to be public and shared in Google Sheets settings (right-click the spreadsheet -> Share -> Get Link -> Anyone in the internet with this link can view).
- Then we add the key of the Google spreadsheet to the modified URL shown below, which is the reference to the Google spreadsheet output as an HTML table.
- Now we open an HTTP
connection request using three arguments: The request method, in this case is a
GET request, the target web page URL, and a third parameter indicating whether the call is asynchronous (False). After that, we send the request. The property readyState equals 4 when the request has successfully completed.
- Then we create a HTML document variable and define the innerHTML of the body as the HTTP response text. At this point, we can navigate the HTML table and get the values. For that we get the HTML elements by table row tags (tr) and then table cell tags (td).
- We use r and c to indicate the corresponding row and column numbers of the table elements in the Excel sheet.
The code above will actually get all the values (the whole table), as no conditions are applied. As an example, a commented line shows how to set a condition to get the first 10 rows only. The condition must be added before the second loop.
Similarly, we could set a condition to get certain columns only. In that case, the condition must be inside the second loop.
If we want to select various non-continuous columns, we could use another variable to differentiate between the source column in the Google spreadsheet table and the recipient column in the Excel worksheet.
This other example shows how to get rows when the value in the third column is 1 (only the first round of games in the example above). Note that we add “Or r = 1” to the condition to always get the header row.
There are several other ways to get specific rows, columns, or cells from Google Sheets. We can use the Google Sheets API, the Google Visualization API, or create our own Google script/API. We will cover that in other posts.
This is how we import specific cells from Google Sheets into Excel with VBA macros.
Other examples:
- How to Unhide All Sheets And Cells with Excel VBA macros
- How to Separate Words And Numbers with Excel VBA macros
- How to Delete Rows Based On Criteria with Excel VBA macros
if spread sheet has more than one sheet,how to import,sir?
ReplyDeleteYou need to use the gid of the sheet you want to import the data from. The gid is shown in the sharing or editing url usually at the end, and is different for each sheet in the Google spreadsheet. You add it to the modified url presented in this video as follows:
ReplyDeletehttps://spreadsheets.google.com/tq?tqx=out:html&key=" & key & "&gid=" & gid