Auto Transfer data from Google sheets to Excel at a set time interval. Help Please.

novalion

New Member
Joined
Aug 16, 2022
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
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. :)




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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I'm surprised you're still getting that message after waiting for 20 seconds. What happens if you just try to run it manually? (ie. no loop)
 
Upvote 0
I'm surprised you're still getting that message after waiting for 20 seconds. What happens if you just try to run it manually? (ie. no loop)
It works fine. It updates to the column like it should. Normally takes like 2 seconds.
 
Upvote 0
I checked the MS documentation, and it seems as thought Wait should allow background processes to keep going for the duration, but maybe try the following routine instead:

VBA Code:
Public Sub PAUSE(Period As Single)

    Dim t As Single
    t = Timer
    Do
        DoEvents
    Loop Until t + Period < Timer

End Sub

So instead of the Application.Wait line of code, try: Pause 5

That works out to be around a 5 second pause, so you may have to try adjusting it up and down till it gets to the right period.
If that doesn't work, I might have one other idea...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top