mysterious web querying problem

platinumpimp68plus1

New Member
Joined
Jul 27, 2010
Messages
2
I'm trying to write a macro that dynamically web queries google analytics data. It was working perfectly fine last week, but suddenly it gives me the "Run-time error '1004': The file could not be accessed. Try one of the following: Make sure the specified folder exists. Make sure the folder that contains the file is not read-only. Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *. Make sure the file/path doesn't contain more than 218" and the debugger highlights "Refresh BackgroundQuery:=False"

But heres the strange thing: it works every other time that I run it... ?

I thought maybe the bad characters in the URL were causing the problem, so I escaped them, but the problem still prevails.

Here is my code (note I altered the url a bit):

Code:
Sub GoogleAnalytics()

Dim date0, date1, date2, date3, LastRow, LastMonth, LastYear, MYURL As String


Range("A5:G40").Delete

' Clears old data

LastMonth = DateAdd("m", -1, Date)
LastYear = DateAdd("yyyy", -1, Date)
date0 = Format(Date, "yyyymmdd")
date1 = Format(DateAdd("d", -7, Date), "yyyymmdd")
date2 = Format(DateAdd("d", -7, LastMonth), "yyyymmdd")
date3 = Format(DateAdd("m", -1, Date), "yyyymmdd")

' Dynamic date ranges for the data I want to extract

MYURL = "https://www.google.com/analytics/reporting/custom" & date1 & "-" & date0 & "moreurl" & date2 & "-" & date3 & "endofurl"

Set qt = ActiveSheet.QueryTables.Add(Connection:= _
        "URL;" & MYURL _
        , Destination:=Range("B5"))
        
    With qt
        .RefreshPeriod = 0
        .BackgroundQuery = True
        .SaveData = True
        .AdjustColumnWidth = False
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = """f_table_data"""
        .Refresh BackgroundQuery:=False
    End With

End Sub

Its driving me crazy, so any help is greatly appreciated!!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Watch MrExcel Video

Forum statistics

Threads
1,133,278
Messages
5,657,813
Members
418,414
Latest member
ECMdusty

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
Top