Hi
I have created a Workbook_Open macro that reads in the contents of a text file, makes some modifications, saves the modified spreadsheet, without the VBA code, to another workbook without the VBA code in it, and then closes the workbook. This all works ok!
Is it possible, within the macro, to delete the originally opened workbook (nnnnBSEXPORT.XLS) after having saved as nnnnBS.XLS? I don't think so because WIndows will still have the file open, but I have been asked if it can be done.
Many thanks
Roger
I have created a Workbook_Open macro that reads in the contents of a text file, makes some modifications, saves the modified spreadsheet, without the VBA code, to another workbook without the VBA code in it, and then closes the workbook. This all works ok!
Code:
Private Sub Workbook_Open()
Dim sFileName As String
Dim sPrefix As String
Dim sOutput As String
Dim sInput As String
Dim i As Integer
'Get This Spreadsheet's prefix - assumes loaded as nnnnBSEXPORT.XLS, and then get input and export filenames.
sFileName = UCase(ThisWorkbook.FullName)
sInput = Left(sFileName, Len(sFileName) - 3) & "TXT"
'Check file exists
If Len(Dir(sInput)) = 0 Then
MsgBox sInput & " NOT found"
Exit Sub
End If
'Get the prefix and save it in export xls filename
sPrefix = Right(sFileName, Len(sFileName) - InStrRev(sFileName, "\"))
i = InStr(1, sPrefix, "BSEXPORT.XLS")
sOutput = ThisWorkbook.Path & "\" & Left(sPrefix, i - 1) & "BS.XLS"
'Now check it doesn't exist - if so quit
If Len(Dir(sOutput)) > 0 Then
MsgBox sOutput & " Already exists - cannot continue"
Exit Sub
End If
'All OK so read in the text file
Workbooks.OpenText Filename:=sInput, Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=True, Comma:=False, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), Array(2, 2), Array( _
3, 2), Array(4, 2), Array(5, 2), Array(6, 2), Array(7, 2), Array(8, 2), Array(9, 2), Array(10 _
, 2), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 2), Array(15, 2), Array(16, 1), _
Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 2), Array(21, 1), Array(22, 2), _
Array(23, 2), Array(24, 2), Array(25, 2), Array(26, 2), Array(27, 2), Array(28, 2), Array( _
29, 2), Array(30, 2), Array(31, 2), Array(32, 2), Array(33, 2), Array(34, 2), Array(35, 2), _
Array(36, 2), Array(37, 2), Array(38, 2), Array(39, 2), Array(40, 2), Array(41, 2), Array( _
42, 2), Array(43, 2), Array(44, 2), Array(45, 2), Array(46, 2), Array(47, 2), Array(48, 2), _
Array(49, 2), Array(50, 2))
Range("A1:IV1").Select
Selection.Font.Bold = True
Columns("A:IV").EntireColumn.AutoFit
Range("A1").Select
'Save work sheet as new name
ActiveWorkbook.SaveAs Filename:=sOutput, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
'Quit
Application.Quit
End Sub
Is it possible, within the macro, to delete the originally opened workbook (nnnnBSEXPORT.XLS) after having saved as nnnnBS.XLS? I don't think so because WIndows will still have the file open, but I have been asked if it can be done.
Many thanks
Roger