VBA Code to refresh data no longer works

MR Campbell

Board Regular
Joined
Aug 2, 2002
Messages
113
Ok ... for some time I have had a puzzling issue with an Excel macro enabled workbook that contains a single SQL query to another database. I cannot stress more that for YEARS it worked with no problems at all. Then one day it just does not work anymore .... the code runs but it does not retrieve the latest data. No error messages are generated, so you are led to thinking that it has updated but it hasn't updated the data at all. The only way I can get it to work now is to physically click on Data then click on the REFRESH ALL. This workbook only has ONE query to a database.

This is the code I used for years (then one day in 2016 it did not work .. no error message at all):
Sheets("Data").Range("a2").QueryTable.Refresh BackgroundQuery:=False

Then I tried this and it worked for a while (this was obtained from the macro recorder):
ActiveWorkbook.Connections("Query from SynergyOne").Refresh

This failed (still no error message ... it just didn't do anything).
Then I tried (this worked for about 6 months)

ActiveWorkbook.RefreshAll

This worked up until July 2016.

So at the moment to get the workbook to execute the refresh of the query, I have to physically click on REFRESH ALL. I would be extremely grateful to anyone who can shed some light on this problem.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this instead

Code:
Sheets("Data").ListObjects(1).Refresh
 
Upvote 0
Do you know why I was having the problems I was experiencing?
- a minor corruption?
- a compatability issue? (was this an xls that is now xlsm?)

These all work for me
Code:
    ActiveWorkbook.RefreshAll
    ActiveWorkbook.Connections(1).Refresh
    ActiveWorkbook.Connections("Query - From SynergyOne").Refresh
    Sheets("Data").ListObjects(1).Refresh
    Sheets("Data").ListObjects("From_SynergyOne").Refresh
    Sheets("Data").Range("A2").ListObject.Refresh


This does not work for me either - if it worked for you in the past, then Microsoft have changed something
Code:
    Sheets("Data").Range("A2").QueryTable.Refresh BackgroundQuery:=False
but the same syntax using ListObject (ie an Excel Table) does work
Code:
    Sheets("Data").Range("A2").ListObject.Refresh

Running this may possibly tell you something (but I suspect not)
Code:
Sub ListConnections()
    Dim a As Long, aList As String, c As Long
    With ActiveWorkbook
        c = .Connections.Count
        For a = 1 To c
         aList = aList & vbCr & .Connections(a)
        Next a
    End With
    MsgBox aList, , c & "Connection(s)"
End Sub

Given the age of your workbook, coupled with the issues you are having, my advice would be to recreate your workbook from a totally CLEAN start and all should be well
 
Last edited:
Upvote 0
This does not work for me either - if it worked for you in the past, then Microsoft have changed something
Code:
    Sheets("Data").Range("A2").QueryTable.Refresh BackgroundQuery:=False
but the same syntax using ListObject (ie an Excel Table) does work
Code:
    Sheets("Data").Range("A2").ListObject.Refresh

I think that Microsoft changing something is at the heart of my problems i.e. code that once worked now doesn't.
My file has always been an .xlsm file.

Many thanks for your generous support with this. I greatly appreciate it.
 
Upvote 0

Forum statistics

Threads
1,213,585
Messages
6,114,520
Members
448,575
Latest member
hycrow

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