Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Access VBA Not Updating Excel
Brandon Jackson 
    
6 years ago
Hello, sir.  I have a block of code that does not update the Excel file it is supposed to during runtime.  It runs and gives no errors, but the file isn't updated.  However, it runs perfectly when stepped through with F8.  Have you ever ran across this before?  I've attached the code block for your reference.

Public Sub UpdateExcel()

    ' Updates Excel file with relevant data from Access queries

    Dim XLFile As String
    Dim XLFolder As String
    Dim NewXLFile As String
    Dim XLApp As Excel.Application
    Dim WB As Excel.Workbook
    Dim CN As Object
    Dim Qry As Object

    XLFile = "Financial Statements.xlsx"
    XLFolder = CurrentProject.Path & "\"
    XLFile = XLFolder & XLFile
    NewXLFile = InputBox("Enter name of file, being sure to start with date.", "File Name")
    If Nz(NewXLFile, "") = "" Then
        Exit Sub
    End If
    NewXLFile = XLFolder & NewXLFile & ".xlsx"
    If Dir(XLFile) = "" Then
        MsgBox "File " & XLFile & " does not exist!", vbExclamation, "File Not Found"
        Exit Sub
    End If
    If Dir(NewXLFile) <> "" Then
        Kill NewXLFile
    End If
    FileCopy XLFile, NewXLFile
    Set XLApp = New Excel.Application
    Set WB = XLApp.Workbooks.Open(NewXLFile)
    WB.RefreshAll
    WB.Worksheets("Income").Select
    Range("E11").Copy
    Range("F11:J11").PasteSpecial xlPasteFormulas
    Range("E1").Select
    DoEvents
    On Error Resume Next
    For Each CN In WB.Connections
        CN.Delete
    Next CN
    For Each Qry In WB.Connections
        Qry.Delete
    Next Qry
    DoEvents
    With WB
        .Save
        .Close
    End With
    Set XLApp = Nothing
    MsgBox "File updated successfully", vbInformation, "Successful Update"

End Sub
Richard Rost  @Reply  
          
6 years ago
Very nice code. It could be anything. Without setting it up and playing with it, I really don't see anything wrong. However, I suspect that something is happening in the background (maybe your FileCopy) that isn't finishing before the code continues. I would recommend placing a Sleep Timer in your code to give it time to process. If it works while stepping through it, that could fix your issue. Keep me posted!
Brandon Jackson OP  @Reply  
    
6 years ago
Hello sir,

     I moved the refreshall command to on open event in the corresponding workbook and everything works nicely. This really helped the timing of the code.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 2:06:45 AM. PLT: 0s