Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:37:56+00:00 2026-06-10T03:37:56+00:00

I have made a code in vba which works with different Workbooks and Worksheets

  • 0

I have made a code in vba which works with different Workbooks and Worksheets. This code must be execute in a scheduled task. But for an unknow reason I have a problem with it :

When I execute it manually, it works fine and excel closes itself. But with my scheduled task, Excel closes all Workbooks and Worksheets but it stays open.
Here you have my code :

    Set xlApp = GetObject(, "excel.application")
    Set wkbMe = xlApp.ActiveWorkbook
    test = False

    xlApp.DisplayAlerts = False
    xlApp.AskToUpdateLinks = False

    'Open files
    xlApp.Workbooks.Open Filename:=MarketDataPath & WbRiskedge, ReadOnly:=True
    xlApp.Workbooks.Open Filename:=MarketDataPath & WbMarketData, ReadOnly:=True
    Set WorksheetIncoming = xlApp.Workbooks(WbMarketData).Worksheets(wsIncoming)
    Set WorksheetMarketdata = xlApp.Workbooks(WbMarketData).Worksheets(WsMarketData)

    xlApp.Workbooks.Open Filename:=GTPath & WbGoodTime, ReadOnly:=True
    Cells.Copy
    WorksheetIncoming.Activate
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                                            :=False, Transpose:=False
    Workbooks(WbGoodTime).Close
    WorksheetMarketdata.Calculate
    Worksheets(wsMarketDataForWebsite).Calculate
    Worksheets(wsMarketDataForWebsite).Activate

    If test = False Then
        Application.Run "MarketEnv.xlsm!subCreateCSV"
    End If
    Workbooks(WbMarketData).Close , False
    Workbooks(WbRiskedge).Close , False
    xlApp.DisplayAlerts = True
    xlApp.AskToUpdateLinks = True
    ThisWorkbook.Save
    ThisWorkbook.Saved = True
    xlApp.Quit
    End Sub

I have tried different solutions found on the web but nothing work. Even if I only make :

 Set xlApp = GetObject(, "excel.application")
 xlApp.Quit
 End Sub

my excel stays open.

Anyone can help me plz ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:37:58+00:00Added an answer on June 10, 2026 at 3:37 am

    Okay, this seems long winded… but here goes.

    The problem you are seeing is because there can be more than one instance of the Excel Application on a machine at a given time.

    When you are manually running the file, you are likely using the default behavior, which is that when you open a workbook directly it opens in your already loaded Excel Application.

    This means that when you use:

    Set xlApp = GetObject(, "excel.application")
    

    It is actually returning the current Excel.Application.
    However, when you load it via the scheduled task, it generates a new Excel.Application in order to handle your task. When it calls the code I quoted it ends up referencing the Excel.Application you already had open (probably).

    Since your scheduled workbook is running in a different instance of Excel.Application, when you run xlApp.Quit it only quits that other Excel and not the one actually running the code.

    If you want to also close your automated Excel, you will need to add Application.Quit to the end of your sub. Yes, I do mean use both xlApp.Quit AND Application.Quit.

    Now technically, you could have more than one Excel Applications open when you load the new one. If you want to close all instances of Excel, the simplest way I know would be to kill all of them via a vbscript call to a program like this which terminates all processes named excel.exe. Note I did not test this on Win 7:

    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strProcessKill, strFilePath
    strComputer = "."
    strProcessKill = "'excel.exe'" 
    
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
    
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = " & strProcessKill)
    For Each objProcess In colProcess
            objProcess.Terminate
    Next
    WScript.Quit
    

    Edit: I just wanted to add that you can replicate the behavior of the scheduled task manually by doing the following:

    1. Have Excel already open.
    2. Navigate through your start menu and open Excel.
    3. From the new instance of Excel, open your workbook.

    OR

    1. Have Excel already open.
    2. Run the following Excel.exe [path of workbook in quotes]

    Edit 2:
    Due to your request, I’ve written this short vbscript file that will close all open Excel Applications without upsetting auto-recover. If you also want to avoid any, “Do you want to save …” alerts uncomment the commented section.

    On Error Resume Next
    Dim xlApp
    Set xlApp = GetObject(, "Excel.Application")
    Do While Err.Number <> 429
        'For each wb in xlApp.Workbooks
        '    wb.saved = true
        'next
        xlApp.Quit
        Set xlApp = Nothing
        Set xlApp = GetObject(, "Excel.Application")
    Loop
    Wscript.quit
    

    To run it, just include the following at the end of your Excel VBA.

    Shell "wscript.exe [path of .vbs file]"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made some code to do this, but as It doesn't work. I
I have made this code for giving out +1 point, but it doesn't work
I have made code but I want it to be execute repeatedly till the
I have made this code working but I want to have an alert ,
I have made this code: var foo=document.createElement(div); var childs=foo.getElementsByTagName(*); console.log(childs.length);//0 OK var a=document.createElement(a); foo.appendChild(a);
I have made this code to open a database(created in SQLite browser) stored in
I have made a simple code for ajax to call a page but it
I have made a YUI module a little like the code shown below, which
I have made some code which exports some details of a journal article to
I have made this code(it is a part of my whole script) -- if(unset($_SESSION['USER_ID']))

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.