The name of the sheet is Importing Data to Excel. The dataset includes Sales Rep, Product Name, Unit, and Sales.
Step 1 – Change the Privacy of Google Sheets
Steps:
- Click Share.
The Share “Importing Data to Excel” dialog box is displayed.
- Click the drop-down arrow in General access.
- Select Anyone with the link.
- Click Done.
Step 2 – Enter the VBA Code to Import Data to Excel
Steps:
- Open a new workbook.
- Go to the Developer tab.
- Click Visual Basic in Code.
In the Microsoft Visual Basic for Applications window:
- Go to the Insert tab.
- Select Module.
- Enter the following code in the module.
Sub Import_Sheets_to_Excel()
Dim QRT As QueryTable, ul As String, ky As String
If ActiveSheet.QueryTables.Count > 0 Then ActiveSheet.QueryTables(1).Delete
ActiveSheet.Cells.Clear
ky = "1slumgR5et-cG7Er-9udweCSthufb7xYdsmoP9H2AOFs"
ul = "https://spreadsheets.google.com/tq?tqx=out:html&key=" & ky
Set QRT = ActiveSheet.QueryTables.Add(Connection:="URL;" & ul, _
Destination:=Range("$A$4"))
With QRT
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.Refresh
End With
End Sub
- The different variables are declared.
- The If logical statement is added to clear tables or data in the worksheet.
- The ky variable is set: it’s the Google spreadsheet key. You can obtain this key from the sheet’s sharing link on the address bar. Locate it after “/d/” and before the next forward slash. See the address below:
https://docs.google.com/spreadsheets/d/1slumgR5et-cG7Er-9udweCSthufb7xYdsmoP9H2AOFs/edit#gid=0
The bold part is the spreadsheet key.
- Set the output of the Google spreadsheet data as an HTML table with a modified URL:
“https://spreadsheets.google.com/tq?tqx=out:html&key=” & ky
- The query is added to the Excel sheet and the destination range is defined: A4.
- Run the code by clicking the play button on the ribbon.
- Go back to the worksheet: Sheet1.
Excel is getting the data from an external source.
This is the output.
Read More: How to Use QUERY Function of Google Sheets in Excel
Step 3 – Apply Formatting to the Dataset
Steps:
- Create a heading in B2. It’s formatted in Heading 2 Cell Style.
- Select B4:E14.
- Go to the Home tab.
- Click the Middle Align icon and the Center icon in Alignment.
- Set All Borders.
This is the output.
- Select the headings in B4:E4 and make them Bold. Change the Fill Color and Font Color.
Download Practice Workbook
Download the Excel workbook.
Related Articles
- How to Import Data from Google Sheets to Excel
- How to Download Google Sheets to Excel
- [Fixed!]: Google Sheet Downloaded as Excel Is Not Working
<< Go Back to Import Google Sheets to Excel | Importing Data in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
How do I import the data from other sheets other than the sheet1 of googlesheet?
Hello, OBOT!
Thanks for your comment!
To import data from a specific sheet (e.g., “Sheet2“) of a Google Sheets document to Excel using VBA, you can use the following code:
To use this code, you need to replace the googleSheetsURL variable with the URL of the Google Sheets document containing the data you want to import and replace the sheetName variable with the name of the sheet containing the data you want to import (in this example, “Sheet2“). You also need to set the targetRange variable to specify the cell or range where you want to paste the imported data (in this example, cell A1 of the Sheet1 worksheet).
The code uses the MSXML2.XMLHTTP object to send an HTTP request to the Google Sheets document, and parses the HTML response to identify the range of cells corresponding to the specified sheet name. It then copies the data from the identified range to the clipboard, clears the target range to ensure that no existing data interferes with the import, and pastes the data from the clipboard into the target range.
Hope this will help you!
Good Luck!
Regards,
Sabrina Ayon
Author, ExcelDemy.
Hello dear
Thanks you…
Is it possible to write a VBA program to upload data from an Excel file directly to Google Sheets?
Hello REZA,
Thanks for commenting and asking your valuable question. Actually, it’s quite costly to do it with VBA. Because of this, you have to enable the Google Sheets API, and quite expensive. So I wouldn’t suggest it.
Rather, you can use Google Drive or the Import feature of Google Sheets to do this task easily. You can follow this linked article on our website to get the whole idea.
Anyway, if you want to do it with VBA, you can follow this lengthy process:
Just make sure to change specific things in your own code. Hope this could help you. Again, thanks to you.
Regards
SHAHRIAR ABRAR RAFID
Team ExcelDemy
this doesnt work
Hello ROBERT,
Do you have Google API? I think the code works. But if it doesn’t work for you, you can use the simple Import feature which I mentioned in the previous reply.
Even if you want to do it with VBA, you can do it through Google Forms. This process doesn’t require the API key.
If you have further queries, you can comment below. Thanks for your feedback.
Regards
SHAHRIAR ABRAR RAFID
Team ExcelDemy
Hello – this is amazing information. I ahve been strugling to get it to work for ages. Can i shoose which rage to extract from the google sheet?
Hello GEORGI,
Thanks for appreciating our work. We are also happy to release you from prolonged suffering. Now, get back to your query.
Sure, you can alter the “Destination” parameter of the “QueryTables.Add” method in your script to specify a specific range to extract from the Google sheet.
For instance, you could change the code such that it retrieves data from cells A1 to E10 of the sheet:
The “ul” variable in this modified code has the addition “&range=A1:E10” to identify the range to extract from. Also, the starting cell where the extracted data will be stored is specified by the “Destination” parameter in the “QueryTables.Add” method, which is set to “$A$4“.
I hope this helps. I wanted to personally invite you to check out our new Excel-related forum. We’ve created a space for Excel enthusiasts like us to share tips, tricks, and ideas, as well as to ask and answer questions about using Excel. We’re a growing community of Excel users, and we’d love to have you join us!
Here’s the link to our forum: ExcelDemy Forum
Regards,
SHAHRIAR ABRAR RAFID
Team ExcelDemy
Thank you!
I want to get data from Excel 365 (web) to Excel by VBA (the same)?
Hello HUONGPT,
Thanks for sharing your experience with us!
The code shared in this article is designed to import data from a Google Sheets web version to an Excel offline version. It uses a Google Sheets key to access the data.
To modify this code for Microsoft 365 Excel online version, you would need to adapt it to use the appropriate URL for your Microsoft 365 Excel file. Microsoft 365 Excel doesn’t use a key like Google Sheets, but rather the URL of the shared document. Use the below VBA code instead:
You can also import data from online Excel without VBA quite easily. Here is how:
1. When you open your Online Excel file there is an option called Edit.
2. Select Edit in Online Excel >> then it will open in Desktop Excel.
3. Now, you will have all the options. It is as easy as it sounds!
Hope these suggestions help. Keep excelling.
Regards,
Yousuf Khan Shovon
Thank you!
But the code not work with my ULR, get message box “This Web Query returned no data…”. I get URL from “Share” link in Excel web.
Hello HUONGPT
Thanks for sharing your problem. You want to get data from Excel 365 (WEB) to a local Excel application. Fencing data from Excel 365 (WEB) is different from importing data from Google Sheets. I am presenting an Excel VBA sub-procedure. However, you must have valid Windows Security credentials on your own.
Excel VBA Sub-procedure:
After Running the code, the Windows Security dialog box will appear => Next, insert the intended username and password => Hit OK.
Hopefully, the idea will help you. Good luck!
Regards
Lutfor Rahman Shimanto
Thank for support!
Hello Huongpt,
You are welcome.
Regards
ExcelDemy