+ Reply to Thread
Results 1 to 2 of 2

VBA Code Not Refreshing External Query Data in Excel 2010

Hybrid View

  1. #1
    Registered User
    Join Date
    08-17-2012
    Location
    Windsor, Canada
    MS-Off Ver
    Excel 2010
    Posts
    1

    VBA Code Not Refreshing External Query Data in Excel 2010

    Hi All

    I have a small Excel file with VBA code. We use the task scheduler in Windows to open this small file which then runs a macro to open, refresh, and save various reports that we have automated. The file works fine in Windows 2000 and Excel 2003, but we are converting to Windows 7 and Excel 2010. The task scheduler opens the small file which then then opens the appropriate report. It looks like it refreshes, and then it deletes the prior report and saves the new report to a particular location. But when I open the saved report, the data from the external query has not changed. If I run the same report manually by opening up the report and hitting refresh all, the data does get updated. Also I do not encounter any errors in the VBA. Here is the code:

    Private Sub Workbook_Open()
        Dim wrkNewBook As Workbook
        Dim wrkOldBook As Workbook
        Dim iCount As Integer
        Dim sFileName As String
        Dim sReportName As String
        Dim iSheetCount As Integer
        Dim sWrapperName As String
    
        sWrapperName = ActiveWorkbook.Name
    
        sFileName = CStr(Application.Workbooks(sWrapperName).Sheets(1).Range("FileName").Value)
        iSheetCount = CLng("0" & Application.Workbooks(sWrapperName).Sheets(1).Range("NumberOfWorkSheets").Value)
    
        Set wrkOldBook = Application.Workbooks.Open(sFileName)
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        ActiveWorkbook.RefreshAll
        Application.Calculate
    
        If iSheetCount > wrkOldBook.Sheets.Count Then
            iSheetCount = wrkOldBook.Sheets.Count
        End If
    
        While (wrkOldBook.Sheets.Count > iSheetCount)
            wrkOldBook.Sheets(wrkOldBook.Sheets.Count).Delete
        Wend
    
        Application.ScreenUpdating = True
        Application.Calculate
    
        wrkOldBook.SaveAs "W:\SHAREU\Finance\POLR 500 Reports\" & wrkOldBook.Name
        wrkOldBook.SaveAs "E:\PUB\EEP\Finance\Cost Reports\" & wrkOldBook.Name
        wrkOldBook.Close False
        Application.DisplayAlerts = True
    
        Application.Quit
    End Sub
    Is there something I need to change so that it will work in Excel 2010?

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA Code Not Refreshing External Query Data in Excel 2010

    Welcome to the forum.

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. If you need more information on how to use them, check my signature.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1