Import Data From Google Sheets via Web Query Excel VBA Macro

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Import Data From Google Sheets via Web Query Excel VBA Macro

Post by adam »

Is there any possibility to get selected data from the google sheet instead of the whole data each time the user runs the macro?

For example,
the user types date in textbox1 and search text in textbox 2 and the related data from google sheet gets copied to excel sheet.
textbox1: 8/21/2015
textbox2: SEVILLA

Code: Select all

Sub QueryGoogleSheets()
      Dim qt As QueryTable, url As String, key As String, gid As String
 
      If ActiveSheet.QueryTables.Count > 0 Then ActiveSheet.QueryTables(1).Delete
      ActiveSheet.Cells.clear
 
      key = "1WopmB1ExWSNul9sw9n7I9FEyquS4AyEugt_fKQrHJSI"
      url = "https://spreadsheets.google.com/tq?tqx=out:html&key=" & key
 
      'if needs to include gid
      gid = "2009384141"
      url = url & "&gid=" & gid
 
      Set qt = ActiveSheet.QueryTables.Add(Connection:="URL;" & url, _
      Destination:=Range("$A$1"))
 
      With qt
          .WebSelectionType = xlAllTables
          .WebFormatting = xlWebFormattingNone
          .Refresh
      End With
  End Sub
Best Regards,
Adam