Downloading Google Sheets is easy as a click.
Sub DownloadGoogleSheets()
Dim ShtUrl, Location, FileName As String
Dim objWebCon, objWrit As Object
'Sheet Url
ShtUrl = "https://docs.google.com/spreadsheets/d/1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY/export?format=csv&id=1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY&gid=0" 'Need to replace id and gid
'Location
Location = ThisWorkbook.Path & "\" 'C:\Export\" Replace with location
'FileName
FileName = "GoogleSheet.csv"
'Connection to Website
Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")
'Writer
Set objWrit = CreateObject("ADODB.Stream")
'Connecting to the Website
objWebCon.Open "Get", ShtUrl, False
objWebCon.Send (ShtUrl)
'Once page is fully loaded
If objWebCon.Status = 200 Then
'Write the text of the sheet
objWrit.Open
objWrit.Type = 1
objWrit.Write objWebCon.ResponseBody
objWrit.Position = 0
objWrit.SaveToFile Location & FileName
objWrit.Close
End If
Set objWebCon = Nothing
Set objWrit = Nothing
End Sub
See it in Action
Sub DownloadGoogleSheets()
Dim ShtUrl, Location, FileName As String
Dim objWebCon, objWrit As Object
'Sheet Url
ShtUrl = "https://docs.google.com/spreadsheets/d/1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY/export?format=csv&id=1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY&gid=0" 'Need to replace id and gid
'Location
Location = ThisWorkbook.Path & "\" 'C:\Export\" Replace with location
'FileName
FileName = "GoogleSheet.csv"
'Connection to Website
Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")
'Writer
Set objWrit = CreateObject("ADODB.Stream")
'Connecting to the Website
objWebCon.Open "Get", ShtUrl, False
objWebCon.Send (ShtUrl)
'Once page is fully loaded
If objWebCon.Status = 200 Then
'Write the text of the sheet
objWrit.Open
objWrit.Type = 1
objWrit.Write objWebCon.ResponseBody
objWrit.Position = 0
objWrit.SaveToFile Location & FileName
objWrit.Close
End If
Set objWebCon = Nothing
Set objWrit = Nothing
End Sub
See it in Action
Excellent code, Shakti. Thanks for sharing. I was able to run in from Access by setting Location to a literal, and to download in Excel format simply by changing "format=csv" to "format=xlsx". This took a bit of trial and error since I couldn't find a published list of formats. Fortunately, "csv" vs "xlsx" is fairly intuitive.
ReplyDeletegreat code. i have a problem: the information in the csv file is mostly gibrish because my data is in hebrew. any suggestions how to copy the data and keep the hebrew?:)
ReplyDeleteShalom Gever,
Deletefor Hebrew and any languages, you need specify UTF-8 format
objWrit.Open
objWrit.Charset = "utf-8"
Thank you for this help.
ReplyDeleteMy question is, I want to download that file as .xls or .xlsx format
Please help me with that.
This comment has been removed by the author.
ReplyDeletees a great piece of code but I have one question.
ReplyDeleteRather than creating a separate file, how can I write the data to a SS that has the VB code?
Hi, works great thank you. What about the other way around ?
ReplyDeleteI am a Typical student and love to work in excel for my Various works. Thanks to post your blog.
ReplyDeleteIt worked! thanks so much <3
ReplyDeleteHello!
ReplyDeleteThis is great code, but I get the runtime 429 error when it tries to CreateObject("MSXML2.XMLHTTP.3.0")
Any suggestions?
hi i got the error of access is denied
ReplyDeletehiiii
ReplyDeletei got an error of access denied please let me
Hi Shakti,
ReplyDeleteNice Work..!!
Can you explain how to upload Files in Google Drive.?
Hi sakthi,
ReplyDeletei am getting the following error, how to resolve it
activex component can't create object in vb6 while creating MSXMS2.XMLHTTP.3.0
Hey, can anyone tell how to download multiple tabs in a singles google sheets using VBA
ReplyDeleteI got also access denied. Can anyone help me with that? Thanks in advance
ReplyDeletei am getting the following error
ReplyDeletethis line
objWebCon.Send (ShtUrl)
Shakti, Great work but, I am stucking
ReplyDeleteSomething error is here,
objWebCon.Open "Get", ShtUrl, False
objWebCon.Send (ShtUrl)
Please help me out
Am also facing same issue.
DeleteAm also facing same issue.
DeleteSomething error is here,
objWebCon.Open "Get", ShtUrl, False
Your blog is too good and informative i need this information thanks for sharing.
ReplyDeleteonline training
Something error is here,
ReplyDeleteobjWebCon.Send (ShtUrl)
>> Run-time error'-2147024891(80070005)' Access is denied
Please help me out
use shareable unik id, insya Alloh working
DeleteHi Shakti, using the same code that you provided, whenever I re-run the macro, the output file should be replaced with the existing one. Because every time I need a refreshed output file, I delete the existing one and then re-run the macro.
ReplyDeleteTried using Application.DiplayAlerts=False/True, but it doesn't work.
Please advise.
Thanks for your great information, the contents are quiet interesting.Keep updating more information from your blog.I will be waiting for your next post
ReplyDeleteTranslation Company In India | Indian Languages Translator | Translation Company In Noida | Certified Translation Services In Pune | Voice-over Services | E-learning Localization
Shakti how can i contact you? do you do consulting work?
ReplyDeleteThanks so much for this. I had this work well for a month or so but then on 25Jun20 about midday in SE Asia it started giving the Access is denied error described at 7:08 in the YouTube video. I dont recall anything changing. I checked my share settings as well as web publishing settings. Anyone have any idea of why this error occurs? NOTE: I did try making a copy of the sheet and changing the share to fit but it fails too. Perhaps Google made some kind of changes somewhere? I'm using 64 bit MS Office Professional Plus 2016 BTW
ReplyDeleteThanks for sharing this helpful information. Its really work.
ReplyDeleteMS Excel Course
JD, i had dame problema in a similar macro to download Google spreadsheet i have used for 2 years, i m looking to know what happened
ReplyDeleteMay have happened after a Microsoft update?
ReplyDeleteSomething error is here,
ReplyDeleteobjWebCon.Send (ShtUrl)
>> Run-time error'-2147024891(80070005)' Access is denied
Already share google sheet. Please help me.
I have the same error, the code worked perfectly up until 2 day ago. Now it shows the same error. Pls help
DeleteI can solve this error change shturl from (
DeleteShtUrl = "https://docs.google.com/spreadsheets/d/1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY/export?format=csv&id=1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY&gid=0" ) to (ShtUrl = "https://docs.google.com/spreadsheets/d/1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY/gviz/tq?tqx=out:csv") . It's work.
This comment has been removed by the author.
DeleteThis comment has been removed by the author.
DeleteFor CSV export data, the direct link "https://docs.google.com/spreadsheets/d/1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY/gviz/tq?
Deletetqx=out:csv") it works fine (Many thanks!). But if you want export to XLX it doesn't work correctly.
My last link was ShtUrl = "https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxx/export?format=xlsx&id=xxxxxxxxxxxx&gid=xxxx", I
tried to change to this link "https://docs.google.com/spreadsheets/d/xxxxxxxxxxx/gviz/tq?tqx=out:xlsx" in order to export to excel
but it not works correctly. I can download the file, but excel find the file corrupted.
Does anyone have the solution for direct download to XLSX?
Many thanks,
Keep:
DeleteShtUrl = "https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxx/export?format=xlsx&id=xxxxxxxxxxxx&gid=xxxx"
And change:
Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")
to:
Set objWebCon = CreateObject("MSXML2.ServerXMLHTTP.3.0")
That works for me, if I want to save it as .xlsx
Many thanks Unknown.
DeleteOnly change Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")to Set objWebCon = CreateObject("MSXML2.ServerXMLHTTP.3.0")and it works fine. Thanks!
Thanks.
DeleteCreateObject("MSXML2.XMLHTTP.3.0")to Set objWebCon = CreateObject("MSXML2.ServerXMLHTTP.3.0")
Works for me ..!
Nice blog! Blog is very useful for everyone. Trunao provides convert your excel spreadsheets to a web app and access your online database anytime, anywhere.
ReplyDeleteIs there a way to incorporate the login and password into this code to download a non-public GSheet?
ReplyDeletewhen i am running the macro it is saving it as a "file" and i cannot use. Please help
ReplyDeleteThis is awesome, thanks Shakti!
ReplyDelete