Solved

Refresh Data Query error

Posted on 2011-02-18
4
304 Views
Last Modified: 2012-05-11
Hi,

I am trying to refresh an OLE DB query with some code.

I get the attached error whenever i run it my routine. The line of code it breaks on is

   Selection.QueryTable.Refresh BackgroundQuery:=False

I can phsyically refresh it on the spreadsheet, just not in the code.

Does anyone know why this is?

Thanks
Seamus
Doc1.doc
0
Comment
Question by:Seamus2626
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:SmittyPro
ID: 34928035
It's probably due to the Selection.  Try referencing the sheet object directly.

HTH,
0
 

Author Comment

by:Seamus2626
ID: 34928092
Heres my complete code, the sheet is called Raw Data, can you suggest?

Thanks
Seamus

    Sheets("Raw Data").Visible = True
    Sheets("Raw Data").Select
    Range("a22").Select
    Selection.QueryTable.Refresh BackgroundQuery:=False
    Range("a24").Select
    Sheets("Screen").Select
       
0
 
LVL 4

Accepted Solution

by:
SmittyPro earned 500 total points
ID: 34928311
For Excel 2007+:

Sheets("Raw Data").Range("A22").ListObject.QueryTable.Refresh BackgroundQuery:=False

For 2003 -:

Sheets("Raw Data").Range("A22").QueryTable.Refresh BackgroundQuery:=False

And if all else fails:

ActiveWorkbook.RefreshAll

The sheet doesn't need to be visible/unhidden.


0
 

Author Closing Comment

by:Seamus2626
ID: 34928346
Good man!

ActiveWorkbook.RefreshAll

That worked

Cheers,
Seamus
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question