Wednesday 15 November 2017

Excel VBA: How to Download Google Sheet

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

43 comments:

  1. 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.

    ReplyDelete
  2. great 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?:)

    ReplyDelete
    Replies
    1. Shalom Gever,
      for Hebrew and any languages, you need specify UTF-8 format

      objWrit.Open
      objWrit.Charset = "utf-8"

      Delete
  3. Thank you for this help.

    My question is, I want to download that file as .xls or .xlsx format

    Please help me with that.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. es a great piece of code but I have one question.

    Rather than creating a separate file, how can I write the data to a SS that has the VB code?

    ReplyDelete
  6. Hi, works great thank you. What about the other way around ?

    ReplyDelete
  7. I am a Typical student and love to work in excel for my Various works. Thanks to post your blog.

    ReplyDelete
  8. Hello!
    This is great code, but I get the runtime 429 error when it tries to CreateObject("MSXML2.XMLHTTP.3.0")

    Any suggestions?

    ReplyDelete
  9. hi i got the error of access is denied

    ReplyDelete
  10. hiiii

    i got an error of access denied please let me

    ReplyDelete
  11. Hi Shakti,
    Nice Work..!!
    Can you explain how to upload Files in Google Drive.?

    ReplyDelete
  12. Hi sakthi,
    i am getting the following error, how to resolve it
    activex component can't create object in vb6 while creating MSXMS2.XMLHTTP.3.0

    ReplyDelete
  13. Hey, can anyone tell how to download multiple tabs in a singles google sheets using VBA

    ReplyDelete
  14. I got also access denied. Can anyone help me with that? Thanks in advance

    ReplyDelete
  15. i am getting the following error
    this line

    objWebCon.Send (ShtUrl)

    ReplyDelete
  16. Shakti, Great work but, I am stucking

    Something error is here,

    objWebCon.Open "Get", ShtUrl, False



    objWebCon.Send (ShtUrl)


    Please help me out

    ReplyDelete
    Replies
    1. Am also facing same issue.

      Delete
    2. Am also facing same issue.

      Something error is here,

      objWebCon.Open "Get", ShtUrl, False

      Delete
  17. Your blog is too good and informative i need this information thanks for sharing.
    online training

    ReplyDelete
  18. Something error is here,

    objWebCon.Send (ShtUrl)

    >> Run-time error'-2147024891(80070005)' Access is denied

    Please help me out

    ReplyDelete
    Replies
    1. use shareable unik id, insya Alloh working

      Delete
  19. Hi 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.

    Tried using Application.DiplayAlerts=False/True, but it doesn't work.

    Please advise.

    ReplyDelete
  20. 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
    Translation Company In India | Indian Languages Translator | Translation Company In Noida | Certified Translation Services In Pune | Voice-over Services | E-learning Localization

    ReplyDelete
  21. Shakti how can i contact you? do you do consulting work?

    ReplyDelete
  22. Thanks 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

    ReplyDelete
  23. Thanks for sharing this helpful information. Its really work.
    MS Excel Course

    ReplyDelete
  24. 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

    ReplyDelete
  25. May have happened after a Microsoft update?

    ReplyDelete
  26. Something error is here,

    objWebCon.Send (ShtUrl)

    >> Run-time error'-2147024891(80070005)' Access is denied

    Already share google sheet. Please help me.

    ReplyDelete
    Replies
    1. I have the same error, the code worked perfectly up until 2 day ago. Now it shows the same error. Pls help

      Delete
    2. I can solve this error change shturl from (
      ShtUrl = "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.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. This comment has been removed by the author.

      Delete
    5. For CSV export data, the direct link "https://docs.google.com/spreadsheets/d/1Fy8T1FeEDzFX9U8_lQDk0HrLNSDGjTDUZFxlx-PWXbY/gviz/tq?

      tqx=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,

      Delete
    6. Keep:
      ShtUrl = "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

      Delete
    7. Many thanks Unknown.
      Only change Set objWebCon = CreateObject("MSXML2.XMLHTTP.3.0")to Set objWebCon = CreateObject("MSXML2.ServerXMLHTTP.3.0")and it works fine. Thanks!

      Delete
    8. Thanks.
      CreateObject("MSXML2.XMLHTTP.3.0")to Set objWebCon = CreateObject("MSXML2.ServerXMLHTTP.3.0")
      Works for me ..!


      Delete
  27. 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.

    ReplyDelete
  28. Is there a way to incorporate the login and password into this code to download a non-public GSheet?

    ReplyDelete
  29. when i am running the macro it is saving it as a "file" and i cannot use. Please help

    ReplyDelete
  30. This is awesome, thanks Shakti!

    ReplyDelete