Excel not fully closing with VBA code

coretex99

New Member
Joined
Feb 25, 2020
Messages
17
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have some simple code that runs on a button click in Access. It Opens Excel and inputs a couple of values of textboxes.

The issue I am having is that somehow Excel is not closing properly once the button is clicked, causing it to error thereforeafter until I go into Task manger and kill the Excel Task. Am I missing something clear? Thank


VBA Code:
Private Sub Export_Click()
 
    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
    
    Set xls = CreateObject("Excel.Application")
    Set wkb = xls.Workbooks.Open("c:\users\x\desktop\logger\log.csv")
    Set wks = wkb.Worksheets("log")

With xls
    .Visible = False

  ActiveWorkbook.Sheets("log").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 3) = Array(Me.txtProduct.Value, Me.txtQuantity.Value, Me.txtCountUp.Value)

    Set wks = Nothing
    Set wkb = Nothing
    Set xls = Nothing
  
    .ActiveWorkbook.Close True
    
End With
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Move the Close line above where you set the variables to Nothing.
 
Upvote 0
How about
VBA Code:
With xls
    .Visible = False

  ActiveWorkbook.Sheets("log").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 3) = Array(Me.txtProduct.Value, Me.txtQuantity.Value, Me.txtCountUp.Value)
   wkb.Close True
   xls.Quit
    Set wks = Nothing
    Set wkb = Nothing
    Set xls = Nothing
  
    
End With
 
Upvote 0
Nope, Compile Error Expected Function Or Variable on the wkb.close D:

This is so frustrating for something that should be so easy!!
 
Upvote 0
Not sure why you are getting that, as it works for me.
I very rarely use Access, so don't know much about it.
 
Upvote 0
You're not properly qualifying the Excel objects, which is why you end up with orphaned processes running. Try this:

VBA Code:
Private Sub Export_Click()
 
    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
    
    Set xls = CreateObject("Excel.Application")
    Set wkb = xls.Workbooks.Open("c:\users\x\desktop\logger\log.csv")
    Set wks = wkb.Worksheets("log")

    xls.Visible = False
    
    wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1).Resize(, 3) = Array(Me.txtProduct.Value, Me.txtQuantity.Value, Me.txtCountUp.Value)
    wbk.Close True

    Set wks = Nothing
    Set wkb = Nothing
    Set xls = Nothing

End Sub
 
Upvote 0
You're not properly qualifying the Excel objects, which is why you end up with orphaned processes running. Try this:

VBA Code:
Private Sub Export_Click()

    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
   
    Set xls = CreateObject("Excel.Application")
    Set wkb = xls.Workbooks.Open("c:\users\x\desktop\logger\log.csv")
    Set wks = wkb.Worksheets("log")

    xls.Visible = False
   
    wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1).Resize(, 3) = Array(Me.txtProduct.Value, Me.txtQuantity.Value, Me.txtCountUp.Value)
    wbk.Close True

    Set wks = Nothing
    Set wkb = Nothing
    Set xls = Nothing

End Sub

Thanks for this, interestingly enough this now works, and runs each time, however it is still creating a Microsoft Excel process in the background, each time I click it it creates a new one, but it now does not error, so that's progress haha.

Clicking the button 5 times = 5 seperate Excel processes running
 
Upvote 0
Oops - you're missing a line that looks like:

Code:
xls.quit
 
Upvote 0

Forum statistics

Threads
1,223,655
Messages
6,173,610
Members
452,522
Latest member
saeedfiroozei

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