ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA closing one file (https://www.excelbanter.com/excel-programming/277979-vba-closing-one-file.html)

colic

VBA closing one file
 
Hiya I am using a macro to create a workbook in another file. I run it
from one file and then it opens up the other file. But then I want it
to save and close the file im creating the new table in.

I am struggling with what I need to do for this.

I can make it close both files but I want to keep the original file
open and save and close the 2nd file.

Any ideas :)

Sub new_workbook()
Dim ws As Worksheet
Dim newSheetName As String
newSheetName = Sheets(1).Range("D3")
Workbooks.Open FileName:= _
"\\dir\\Test\All Advisors.xls"
For Each ws In Worksheets
If ws.Name = newSheetName Or newSheetName = "" Or
IsNumeric(newSheetName) Then
MsgBox "Sheet already exists or name is invalid",
vbInformation
Exit Sub
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
..Move after:=Worksheets(Worksheets.Count)
..Name = newSheetName
End With
Workbooks.Close
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/


Don Guillett[_4_]

VBA closing one file
 
try
activeworkbook.close

"colic" wrote in message
...
Hiya I am using a macro to create a workbook in another file. I run it
from one file and then it opens up the other file. But then I want it
to save and close the file im creating the new table in.

I am struggling with what I need to do for this.

I can make it close both files but I want to keep the original file
open and save and close the 2nd file.

Any ideas :)

Sub new_workbook()
Dim ws As Worksheet
Dim newSheetName As String
newSheetName = Sheets(1).Range("D3")
Workbooks.Open FileName:= _
"\\dir\\Test\All Advisors.xls"
For Each ws In Worksheets
If ws.Name = newSheetName Or newSheetName = "" Or
IsNumeric(newSheetName) Then
MsgBox "Sheet already exists or name is invalid",
vbInformation
Exit Sub
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
Move after:=Worksheets(Worksheets.Count)
Name = newSheetName
End With
Workbooks.Close
End Sub



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/




John Wilson

VBA closing one file
 
colic,

Activeworkbook.Close
wil close whatever workbook is active at the time.

John

colic wrote:

Hiya I am using a macro to create a workbook in another file. I run it
from one file and then it opens up the other file. But then I want it
to save and close the file im creating the new table in.

I am struggling with what I need to do for this.

I can make it close both files but I want to keep the original file
open and save and close the 2nd file.

Any ideas :)

Sub new_workbook()
Dim ws As Worksheet
Dim newSheetName As String
newSheetName = Sheets(1).Range("D3")
Workbooks.Open FileName:= _
"\\dir\\Test\All Advisors.xls"
For Each ws In Worksheets
If ws.Name = newSheetName Or newSheetName = "" Or
IsNumeric(newSheetName) Then
MsgBox "Sheet already exists or name is invalid",
vbInformation
Exit Sub
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
Move after:=Worksheets(Worksheets.Count)
Name = newSheetName
End With
Workbooks.Close
End Sub

------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/




All times are GMT +1. The time now is 06:49 AM.

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