PC Review


Reply
Thread Tools Rate Thread

close a file ater opening with GetOpenFileName

 
 
Forum Freak
Guest
Posts: n/a
 
      9th Jun 2008
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



 
Reply With Quote
 
 
 
 
Ron de Bruin
Guest
Posts: n/a
 
      9th Jun 2008
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


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Forum Freak" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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
>
>
>

 
Reply With Quote
 
Forum Freak
Guest
Posts: n/a
 
      9th Jun 2008
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


"Ron de Bruin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> --
>
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
>
>
> "Forum Freak" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Detect file opening / close nuttynibbles Microsoft Dot NET Compact Framework 7 24th Feb 2010 09:01 PM
GetOpenFilename Close Karen53 Microsoft Excel Programming 2 16th Mar 2008 06:22 AM
Opening a document captured by Application.GetOpenFilename with hyperlink nilshalvarsson@hotmail.com Microsoft Excel Programming 2 16th May 2007 05:18 PM
Windows MM has encountered problem (opening file) and must close =?Utf-8?B?Qmx1ZVBocm9n?= Windows XP MovieMaker 0 10th Oct 2005 05:06 AM
Opening a file causes Excel to close Stuart Microsoft Excel Programming 1 12th Mar 2004 11:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:30 PM.