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 
Excel File Not Closing After Import
Howard Chapell 
    
7 months ago
Hi I have a routine to import Multiple Worksheets from a Workbook. It generally works the First Time but if I retry for whatever reason it hangs. I go into Taskmanager and find Excel is still loaded. The following is my Code>
DetailsPublic Sub CmdImportAllWorkSheets_Click()

Dim fdialog As FileDialog
Dim varFile As Variant
Dim objXL As New Excel.Application
Dim LastRow As Long
Dim LastCol As Long
Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet
Dim tblName As String
Dim RecordCount As Long
Dim RecStr As String
Dim LastRowStr As String
Dim RecordCountStr As String


    On Error GoTo Cleanup
    ' On Error Resume Next
    
'set up the file Dialog.
Set fdialog = Application.FileDialog(msoFileDialogFilePicker)
With fdialog
    .AllowMultiSelect = False
    .Filters.Add "Excel Files", "*.xls;*.xlsx;*.xlsm"
    If .Show = True Then
        For Each varFile In .SelectedItems
           Set wkb = objXL.Workbooks.Open(varFile)
           tblName = "tblWorkList5"
        For Each wks In wkb.Worksheets
        LastRow = wks.Cells(wks.Rows.Count, 1).End(-4162).Row
        RecordCount = LastRow - 1
        
        LastRowStr = CStr(LastRow)
        RecordCountStr = CStr(RecordCount)
        
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tblName, varFile, True, wks.Name & "$"
                
        Status RecordCountStr
        Status "No Of Records"
        Status LastRowStr
        Status "Last Row"
        Status wks.Name
        Status "WorkSheet Name"
        Status "Commencing Import"
        Sleep 2000
        Next wks
         wkb.Close False
        Next varFile
     End If
End With


Cleanup:
   ' wkb.Close False
   Set wks = Nothing
   Set wkb = Nothing
   objXL.Quit
   Set objXL = Nothing
   '  If Not objXL Is Nothing Then objXL.Quit
   '  Set objXL = Nothing

   End Sub
Richard Rost  @Reply  
          
7 months ago
Two quick fixes. Best: don't start Excel at all - TransferSpreadsheet reads the workbook directly, so loop the sheet names and import each one, no Excel process to hang. If you keep automation, don't use "As New" - use Set ObjXL = CreateObject("Excel.Application"), open ReadOnly, and in your Cleanup always wkb.Close False, Set wks = Nothing, Set wkb = Nothing, ObjXL.Quit, Set ObjXL = Nothing - Excel sticks around if any ref is still alive.
Howard Chapell OP  @Reply  
    
7 months ago
Thanks for your response Richard much appreciated

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/6/2026 5:12:58 PM. PLT: 1s