Excel stays open in Task Manager after closing (VBA)

exceldemon

New Member
Joined
Jun 30, 2010
Messages
38
I have a VBA script from a 3D modeling program I use that opens a Excel Workbook and adds text to it. Everything works just fine but when I close the workbook, Excel still shows up in the Task Manager. If I run my macro again, it doesnt work. If I end the task in the Task Manager, it works again.


Here is the code I use to open the existing Worksheet that hads a header, then imports text.

Code:
Private Function convertexcel()
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet

Set xlApp = New Excel.Application
xlApp.Visible = False
xlApp.DisplayAlerts = False
    ChDir "V:\MyFolder"
    Workbooks.Open FileName:= _
        "V:\MyFolder\ENGR STANDARD.xlsx" _
        , UpdateLinks:=0, Notify:=False
    Range("A12:M15").Select
    Selection.Delete Shift:=xlUp
    Range("D19").Select

With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\tempbom.txt", _
        Destination:=Range("$A$12"))
        .Name = "tempbom"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlFormatFromLeftOrAbove
        .SavePassword = False
        .SaveData = True
        '.AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Rows("12:12").Select
    Selection.Delete Shift:=xlUp
'    Columns("C:C").Select
'    With Selection
'        .HorizontalAlignment = xlLeft
'    End With

    Range("D1").Select

xlApp.Visible = True
End Function
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Perhaps like this

Rich (BB code):
    Range("D1").Select

xlApp.Visible = True
xlApp.Quit
End Function
 
Upvote 0
Perhaps like this

Rich (BB code):
    Range("D1").Select

xlApp.Visible = True
xlApp.Quit
End Function

That exits Excel. I dont want to exit Excel with the macro. The user needs to work on the Worksheet and save it to the desired location.
 
Upvote 0
The actual problem is that you have unqualified object references in your code, so you need to change stuff like

ActiveWorkbook

to

xlApp.ActiveWorkbook

or

Range

with

xlApp.Range

if you don't, then the code will create another Excel instance, which will stay open in the task manager. You need to qualify ALL objects like that.
 
Upvote 0
The actual problem is that you have unqualified object references in your code, so you need to change stuff like

ActiveWorkbook

to

xlApp.ActiveWorkbook

or

Range

with

xlApp.Range

if you don't, then the code will create another Excel instance, which will stay open in the task manager. You need to qualify ALL objects like that.

Ok, I added xlApp. before each object. Is there a way not have to add that before each line? Rather than typing xlApp. before everything?

This works.

Private Function convertexcel()
Dim xlApp As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet

Set xlApp = New Excel.Application
xlApp.Visible = False
xlApp.DisplayAlerts = False
ChDir "V:\MyFolder"
xlApp.Workbooks.Open FileName:= _
"V:\MyFolder\ENGR STANDARD.xlsx" _
, UpdateLinks:=0, Notify:=False
xlApp.Range("A12:M15").Select
xlApp.Selection.Delete Shift:=xlUp
xlApp.Range("D19").Select

With xlApp.ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\tempbom.txt", _
Destination:=xlApp.Range("$A$12"))
.Name = "tempbom"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlFormatFromLeftOrAbove
.SavePassword = False
.SaveData = True
'.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
xlApp.Rows("12:12").Select
xlApp.Selection.Delete Shift:=xlUp
' Columns("C:C").Select
' With Selection
' .HorizontalAlignment = xlLeft
' End With

xlApp.Range("D1").Select

xlApp.Visible = True
End Function
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top