Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query update not working in Workbook_Open() macro

Posted on 2016-11-25
3
Medium Priority
?
46 Views
Last Modified: 2016-12-01
I have a vba script that refreshes an Excel workbook tab with an external connection to a SQL database.  The vba is part of a VBA script that calls separate scripts in an order.  When I run the master VBA script manually, it completes without a problem.

However, when I copy the same VBA script to a private sub Workbook_Open() it gets a run time error 1004:  Select Method of Worksheet Class Failed at the point of the query refresh.

Here is the vba script that throws the error:

' Update Meditech Data
'
    Sheets("DailyStats").Select
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    Sheets("NonExHrs").Select
    Range("A1").Select
    Sheets("DailyFTE").Select

Any ideas why it will not work when the workbook opens?

thanks

Glen
0
Comment
Question by:GPSPOW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Ejgil Hedegaard
ID: 41902302
Try this
Sheets("DailyStats").ListObjects(1).QueryTable.Refresh BackgroundQuery:=False

You don't need to select the sheet to update the query.

You could also set the query to update on workbook open.
0
 

Accepted Solution

by:
GPSPOW earned 0 total points
ID: 41902357
I figured it out.  The macro calls in the auto exec macro were pointing to a closed workbook and creating the error.  I edited the Workbook-Open macro and now it works.

Thanks
0
 

Author Closing Comment

by:GPSPOW
ID: 41908431
Coding error on my part.  Figured it out myself.

Glen
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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

618 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