close a file ater opening with GetOpenFileName

F

Forum Freak

Hi

I open a file with code using GetOpenFileName then copy a sheet to an
already open file.
I have tried altering the code to close the file after the sheet has been
copied but have had no success.

Could someone tell me how to do it?

Many Thanks

Kenny
XP Pro
Office 2003

The code I am currently using is below

Sub OpenAFile()

' thanks MrExcel.com



Dim vFilename As Variant



vFilename = Application.GetOpenFilename("Microsoft Excel
Workbooks,*.xls")



If vFilename = False Then Exit Sub 'User pressed Cancel



Workbooks.Open vFilename




Sheets("Data").Select



Sheets("Data").Copy Before:=Workbooks( _

"Copy of Defects Audit V4.01getopenfilename.xls").Sheets(1)






MyAudit



Sheets("Data").Delete

Sheets("Report").Select

End Sub
 
R

Ron de Bruin

See this tester

I use
Set wb = Workbooks.Open(FName)

Now you can use this after you do the copy
wb.Close False


Sub test()
Dim FName As Variant
Dim wb As Workbook
Dim MyPath As String
Dim SaveDriveDir As String

SaveDriveDir = CurDir

MyPath = ThisWorkbook.Path
ChDrive MyPath
ChDir MyPath

FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls), *.xls")
If FName <> False Then
Set wb = Workbooks.Open(FName)

' do your stuff

wb.Close False
End If

ChDrive SaveDriveDir
ChDir SaveDriveDir

End Sub
 
F

Forum Freak

Many thanks Ron. You have come to my rescue yet again. It worked a treat.
As usual I will credit you and your website in the macro for future
reference.

Regards
Kenny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top