+ Reply to Thread
Results 1 to 7 of 7

efficiently open and close an Excel file in VBA Access

  1. #1
    Registered User
    Join Date
    01-06-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    7

    efficiently open and close an Excel file in VBA Access

    I'm writing a code in vba Access 2002 a button to open an Excel file do some work and close it and it works but if you refresh and clik again the button get an error "object variable or with block not defined" In the Task Manager from Windows I see the Excel process running even if the file is close....the code vba did not kill the process when exiting the code, so I have to close the application and re-start.

    How can I close the file and kill the process in order to open Excel files without exiting the application?

    Thank you

    This is part of the button code:

    Please Login or Register  to view this content.
    Last edited by ucalcen; 01-07-2014 at 01:58 PM. Reason: Putting tags

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: efficiently open and close an Excel file in VBA Access

    Can you add code tags?

    The first thing you should be doing in the code is creating a reference to the workbook you are opening, when you open it.

    You would then use that to reference the workbook throughout the rest of the code.

    One thing you definitely shouldn't be doing is using ActiveCell, that doesn't really mean anything in Access VBA and could actually be connected to the problem you describe.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-06-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: efficiently open and close an Excel file in VBA Access

    Thanks for the reply...I put the tags...

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: efficiently open and close an Excel file in VBA Access

    This is how I would do the code.
    Please Login or Register  to view this content.
    Note I've used oWs.Range("A1") in place of ActiveCell as I assume you want to refer to A1 on 'Feuil1', obviously change that if that assumption is incorrect.

  5. #5
    Registered User
    Join Date
    01-06-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: efficiently open and close an Excel file in VBA Access

    Thanks, now I see the problem....when a set oExcel as a Excel.Application and then in the for loop I transfert line by line to the ACTIVE Excel open not the oExcel Object...it never closes...I try your solution.

  6. #6
    Registered User
    Join Date
    01-06-2014
    Location
    Montreal
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: efficiently open and close an Excel file in VBA Access

    Thanks, it worked your way and also my code by adding oExcel to ActiveCell

    Please Login or Register  to view this content.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: efficiently open and close an Excel file in VBA Access

    Is that all you changed in your original code?

    That might not be a good idea, oExcel is a reference to the Excel application not the workbook.

    Try using explicit references to the workbook/worksheet/range.

    That's what you would do if the code was being run in Excel.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  2. Check if a Word File is Open and If Open Close it with Excel VBA.
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-10-2012, 11:14 AM
  3. [SOLVED] Schedule Excel to open file, print sheets, close file
    By tonychopra194 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-22-2012, 05:15 AM
  4. Need Help with Excel Macro - Open - Close File
    By Lpitt56 in forum Excel General
    Replies: 6
    Last Post: 01-26-2012, 03:31 PM
  5. Close an Excel file if is found it open.
    By maperalia in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-07-2006, 06:42 AM

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