Hi everyone, I am new to VBA. I have been trying to get the below code to work, but I am having some trouble.
What the code should do: Pick the data from a Google Sheet and transfer/update it to the Excel Worksheet every 20 seconds.
Problem: The updating part works fine, the delay works fine as well, however when the loop is added it no longer updates and eventually breaks. I get the message "This operation cannot be done because the data is refreshing in the background".
Any help would be much appreciated. Thanks in advance.
What the code should do: Pick the data from a Google Sheet and transfer/update it to the Excel Worksheet every 20 seconds.
Problem: The updating part works fine, the delay works fine as well, however when the loop is added it no longer updates and eventually breaks. I get the message "This operation cannot be done because the data is refreshing in the background".
Any help would be much appreciated. Thanks in advance.
VBA Code:
Sub ImportGoogleSheetsQuery()
Dim qt As QueryTable, url As String, key As String, gid As String
Dim n As Long
n = 0
Do Until n = 1
If ActiveSheet.QueryTables.Count > 0 Then ActiveSheet.QueryTables(1).Delete
ActiveSheet.Cells.Clear
'***** Shared Google Sheets Link *****
key = "XXXXXXXXXXXXXXXXXXXXXXX"
gid = "0"
url = "https://spreadsheets.google.com/tq?tqx=out:html&key=" & key _
& "&gid=" & gid
Set qt = ActiveSheet.QueryTables.Add(Connection:="URL;" & url, _
Destination:=Range("A1"))
With qt
.WebSelectionType = xlAllTables
.Refresh
End With
Application.Wait DateAdd("s", 20, Now)
Loop
End Sub