?
Solved

Query update not working in Workbook_Open() macro

Posted on 2016-11-25
3
Medium Priority
?
44 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 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