ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Closing Files Opened Using the FileToOpen (https://www.excelbanter.com/excel-programming/383633-closing-files-opened-using-filetoopen.html)

D Zandveld

Closing Files Opened Using the FileToOpen
 
Hi Again

I have opened a file successfully (user defined and stored as FileToOpen)
and copied it's content into the active worksheet.
However, since it opens a new instance of Excel, I can't work out how to
close the FileToOpen file, and where the command belongs in the code below.

-----------------------------------------------------------------------------

Worksheets("Imported Data").Visible = True

With Application.FileDialog(msoFileDialogOpen)
FileToOpen = Application _
.GetOpenFilename("Comma Separated Values (*.csv),*.csv,Microsoft
Excel (*.xls),*.xls,All Files (*.*),*.*")
Workbooks.Open FileToOpen
Range("A1").Activate
Application.Run ("'Report Generator v1.xls'!Select_Data")
Selection.Copy
Workbooks("'Report Generator v1.xls").Activate
Sheets("Imported Data").Activate
Range("A1").Select
Selection.Insert Shift:=xlDown

***I assume here I need to switch to the other workbook
(FileToOpen), close it, then switch back to ("Report Generator v1.xls")***

End With
Worksheets("Imported Data").Visible = False
Sheets("Main").Activate

Tim Williams

Closing Files Opened Using the FileToOpen
 

dim oWB
set oWB = Workbooks.Open(FileToOpen)

' do stuff with oWB

oWB.Close


Tim




"D Zandveld" wrote in message
...
Hi Again

I have opened a file successfully (user defined and stored as FileToOpen)
and copied it's content into the active worksheet.
However, since it opens a new instance of Excel, I can't work out how to
close the FileToOpen file, and where the command belongs in the code
below.

-----------------------------------------------------------------------------

Worksheets("Imported Data").Visible = True

With Application.FileDialog(msoFileDialogOpen)
FileToOpen = Application _
.GetOpenFilename("Comma Separated Values
(*.csv),*.csv,Microsoft
Excel (*.xls),*.xls,All Files (*.*),*.*")
Workbooks.Open FileToOpen
Range("A1").Activate
Application.Run ("'Report Generator v1.xls'!Select_Data")
Selection.Copy
Workbooks("'Report Generator v1.xls").Activate
Sheets("Imported Data").Activate
Range("A1").Select
Selection.Insert Shift:=xlDown

***I assume here I need to switch to the other workbook
(FileToOpen), close it, then switch back to ("Report Generator v1.xls")***

End With
Worksheets("Imported Data").Visible = False
Sheets("Main").Activate




D Zandveld

Closing Files Opened Using the FileToOpen
 
Great Stuff, Thanks Tim.

"Tim Williams" wrote:


dim oWB
set oWB = Workbooks.Open(FileToOpen)

' do stuff with oWB

oWB.Close


Tim




"D Zandveld" wrote in message
...
Hi Again

I have opened a file successfully (user defined and stored as FileToOpen)
and copied it's content into the active worksheet.
However, since it opens a new instance of Excel, I can't work out how to
close the FileToOpen file, and where the command belongs in the code
below.

-----------------------------------------------------------------------------

Worksheets("Imported Data").Visible = True

With Application.FileDialog(msoFileDialogOpen)
FileToOpen = Application _
.GetOpenFilename("Comma Separated Values
(*.csv),*.csv,Microsoft
Excel (*.xls),*.xls,All Files (*.*),*.*")
Workbooks.Open FileToOpen
Range("A1").Activate
Application.Run ("'Report Generator v1.xls'!Select_Data")
Selection.Copy
Workbooks("'Report Generator v1.xls").Activate
Sheets("Imported Data").Activate
Range("A1").Select
Selection.Insert Shift:=xlDown

***I assume here I need to switch to the other workbook
(FileToOpen), close it, then switch back to ("Report Generator v1.xls")***

End With
Worksheets("Imported Data").Visible = False
Sheets("Main").Activate






All times are GMT +1. The time now is 09:12 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com