' Call out variables
Dim fname As String
Dim SheetName As String
Sub CreateOpenFile()
Application.ScreenUpdating = False
' Formatted file name based on Month and Year
fname = "High Volume Scrap " & Format(Date, "yyyy") & ".xls"
SheetName = Format(Date, "mmmm")
' Sets the working directory
ChDir ("C:\Test\")
' Test function to see if the file is there already
If FileExists(fname) = False Then
' Creates File.
Workbooks.Add
' Adds the first workbook
Sheets.Add
ActiveSheet.Name = "Volume and Buy Back " & Format(Date, "yyyy")
Sheets.Add
ActiveSheet.Name = Format(Date, "mmmm")
' Erase First 3 sheets of the Workbook
Application.DisplayAlerts = False
ActiveWorkbook.Worksheets("Sheet1").Delete
ActiveWorkbook.Worksheets("Sheet2").Delete
ActiveWorkbook.Worksheets("Sheet3").Delete
Application.DisplayAlerts = True
' Save the workbook.
ActiveWorkbook.SaveAs Filename:="C:\Test\" & fname, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
' Set the Headers for the Mass Spec Data (Sheet named Format(Date, "mmmm")
Call MassSpecHeaders
Else
' Open the file if it already exists.
On Error Resume Next
Workbooks.Open Filename:="C:\Test\" & fname
End If
' Search the tabs for a current month tab.
If SheetExists(SheetName) = False Then
Sheets.Add
ActiveSheet.Name = Format(Date, "mmmm")
Call MassSpecHeaders ' Inserts Headers for Mass Spec Data
Else
Sheets(SheetName).Select
End If
End Sub
Function FileExists(fname As String) As Boolean
' Returns True if a file exists.
FileExists = Dir(fname) <> ""
End Function
Function SheetExists(ByVal SheetName As String) As Boolean
On Error Resume Next
SheetExists = Not Sheets(SheetName) Is Nothing
End Function