Solved

Query update not working in Workbook_Open() macro

Posted on 2016-11-25
3
16 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
  • 2
3 Comments
 
LVL 21

Expert Comment

by:Ejgil Hedegaard
Comment Utility
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
Comment Utility
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
Comment Utility
Coding error on my part.  Figured it out myself.

Glen
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Viewers will learn how the fundamental information of how to create a table.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now