PC Review


Reply
Thread Tools Rate Thread

Closing a workbook

 
 
Gert-Jan
Guest
Posts: n/a
 
      4th Dec 2006
Hi,

Can anyone help to make this work:

Sub CopyAndClose()
Dim filex
filex = Range("Sheets2!F2").Value
Workbooks.Open Filename:=filex, Password:="123"
Sheets("database").Select
Sheets("database").Copy Before:=Workbooks("test.xls"). _
Sheets(1)
Workbooks.Close Filename:=filex
End Sub

The error is in the last row.

Thanks, Gert-Jan


 
Reply With Quote
 
 
 
 
NickHK
Guest
Posts: n/a
 
      4th Dec 2006
You will find it more easy to give yourself some variables to use.
dim WB as workbook

set wb=Workbooks.Open Filename:=filex, Password:="123"

'Don't what Workbooks("test.xls") refers to ?
Thisworkbook.Sheets("database").Copy Before:=Workbooks("test.xls").Sheets(1)
'Or do you mean
Thisworkbook.Sheets("database").Copy Before:=WB.Sheets(1)
'Or maybe, depending on which workbook contains the sheet "database"
WB.Sheets("database").Copy Before:=Thisworkbook.Sheets(1)
wb.close

NickHK

"Gert-Jan" <(E-Mail Removed)> wrote in message
news:4573f32d$0$2318$(E-Mail Removed)...
> Hi,
>
> Can anyone help to make this work:
>
> Sub CopyAndClose()
> Dim filex
> filex = Range("Sheets2!F2").Value
> Workbooks.Open Filename:=filex, Password:="123"
> Sheets("database").Select
> Sheets("database").Copy Before:=Workbooks("test.xls"). _
> Sheets(1)
> Workbooks.Close Filename:=filex
> End Sub
>
> The error is in the last row.
>
> Thanks, Gert-Jan
>
>



 
Reply With Quote
 
NickHK
Guest
Posts: n/a
 
      4th Dec 2006
That should read :
set wb=Workbooks.Open(Filename:=filex, Password:="123")

and
> 'Don't know what Workbooks("test.xls") refers to ?
> Thisworkbook.Sheets("database").Copy

Before:=Workbooks("test.xls").Sheets(1)

NickHK

"NickHK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> You will find it more easy to give yourself some variables to use.
> dim WB as workbook
>
> set wb=Workbooks.Open Filename:=filex, Password:="123"
>
> 'Don't what Workbooks("test.xls") refers to ?
> Thisworkbook.Sheets("database").Copy

Before:=Workbooks("test.xls").Sheets(1)
> 'Or do you mean
> Thisworkbook.Sheets("database").Copy Before:=WB.Sheets(1)
> 'Or maybe, depending on which workbook contains the sheet "database"
> WB.Sheets("database").Copy Before:=Thisworkbook.Sheets(1)
> wb.close
>
> NickHK
>
> "Gert-Jan" <(E-Mail Removed)> wrote in message
> news:4573f32d$0$2318$(E-Mail Removed)...
> > Hi,
> >
> > Can anyone help to make this work:
> >
> > Sub CopyAndClose()
> > Dim filex
> > filex = Range("Sheets2!F2").Value
> > Workbooks.Open Filename:=filex, Password:="123"
> > Sheets("database").Select
> > Sheets("database").Copy Before:=Workbooks("test.xls"). _
> > Sheets(1)
> > Workbooks.Close Filename:=filex
> > End Sub
> >
> > The error is in the last row.
> >
> > Thanks, Gert-Jan
> >
> >

>
>



 
Reply With Quote
 
Gert-Jan
Guest
Posts: n/a
 
      4th Dec 2006
Hi Nick,

You have given the right solution, thanks a lot.

Best regards, Gert-Jan

"NickHK" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
> That should read :
> set wb=Workbooks.Open(Filename:=filex, Password:="123")
>
> and
>> 'Don't know what Workbooks("test.xls") refers to ?
>> Thisworkbook.Sheets("database").Copy

> Before:=Workbooks("test.xls").Sheets(1)
>
> NickHK
>
> "NickHK" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> You will find it more easy to give yourself some variables to use.
>> dim WB as workbook
>>
>> set wb=Workbooks.Open Filename:=filex, Password:="123"
>>
>> 'Don't what Workbooks("test.xls") refers to ?
>> Thisworkbook.Sheets("database").Copy

> Before:=Workbooks("test.xls").Sheets(1)
>> 'Or do you mean
>> Thisworkbook.Sheets("database").Copy Before:=WB.Sheets(1)
>> 'Or maybe, depending on which workbook contains the sheet "database"
>> WB.Sheets("database").Copy Before:=Thisworkbook.Sheets(1)
>> wb.close
>>
>> NickHK
>>
>> "Gert-Jan" <(E-Mail Removed)> wrote in message
>> news:4573f32d$0$2318$(E-Mail Removed)...
>> > Hi,
>> >
>> > Can anyone help to make this work:
>> >
>> > Sub CopyAndClose()
>> > Dim filex
>> > filex = Range("Sheets2!F2").Value
>> > Workbooks.Open Filename:=filex, Password:="123"
>> > Sheets("database").Select
>> > Sheets("database").Copy Before:=Workbooks("test.xls"). _
>> > Sheets(1)
>> > Workbooks.Close Filename:=filex
>> > End Sub
>> >
>> > The error is in the last row.
>> >
>> > Thanks, Gert-Jan
>> >
>> >

>>
>>

>
>



 
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
Automatically closing slave workbook when main workbook is closed... Potsy Microsoft Excel Discussion 0 9th Mar 2010 11:48 AM
Automatically closing slave workbook when main workbook is closed... Potsy Microsoft Excel Discussion 0 9th Mar 2010 11:46 AM
Closing a workbook from a macro doesn't close the workbook =?Utf-8?B?RGF2ZSBQ?= Microsoft Excel Programming 2 10th Jul 2007 06:16 PM
Closing Hidden Workbook when Active Workbook is Closed =?Utf-8?B?U3VzYW5LNTIx?= Microsoft Excel Programming 5 24th Sep 2005 12:27 AM
closing excel after closing a workbook CWalsh Microsoft Excel Programming 3 21st Jan 2004 03:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:51 AM.