Closing an Excel file using VB

MR Campbell

Board Regular
Joined
Aug 2, 2002
Messages
113
At the moment, there is no compile error or runtime error, but if I manually attempt to open the file (that was successfully read from), I get the message that the file is already open and is locked by myself (the user) and that multiple Excel processes are still running (I checked the task manager).

How do I close this Excel file (no changes to be made since I only read from the file) and quit Excel from running ?

Here is what I have at the moment :

Dim FirstName, Surname As String

'file path and name for the Excel file
Dim FileNamePath As String = "C:\Users\MRC\Desktop\MRC Files\Visual Basic Files\SwimManager 2014\CurrentSeasonDATA\SquadData.xls"

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet

xl = New Excel.Application 'run a new Excel application
xl.Visible = False 'make Excel invisible

wb = xl.Workbooks.Open(FileNamePath) 'open the workbook file
sht = DirectCast(wb.Worksheets("DOB"), Excel.Worksheet) 'define the worksheet

FirstName = sht.Cells(6, 3).Value 'read the first name
Surname = sht.Cells(6, 4).Value 'read the surname

'Write the name to a textbox
TextBox2.Text = FirstName & " " & Surname

wb.Close(SaveChanges:=False, Filename:=FileNamePath) '<==== not sure about these lines from HERE
xl.Workbooks.Close()
xl.Quit()

wb = Nothing
sht = Nothing
xl = Nothing
 

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
When working with objects you need to use the Set statement. e.g.

Code:
[COLOR=#ff0000]Set[/COLOR] xl = New Excel.Application      'run a new Excel application
 
Upvote 0
Teeroy
The OP is using VB.NET, and there's no "Set" keyword.
 
Upvote 0
Teeroy
The OP is using VB.NET, and there's no "Set" keyword.

Sektor, you are correct; I'd assumed VBA. I don't use VB.NET and rarely use VBScript so I'd forgotten the differences.
 
Upvote 0
Teeroy

I already answered him here, but, for some reason, OP decided to start new thread. :)
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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