Hi all:
I am trying to close a text file by name that was opened in excel but unable to do so. Here is scenario:
I have a workbook A open that has button which triggers macro/vba select and open text file in excel.
Get data from opened text file and then close it without saving. Close by name, not by file number. I have a code that is working fine if I close text file by file number, but I am unable to close it by name. Reason to close it by name is, if by chance I have 2 workbooks open (e.g. workbook A, related to text file. And workbook B, not related to A or text file). If I close text file by file number than it closes workbook B and keeps open text file (opened in excel). Here is my code:
Any help would be appreciated.
Thanks,
Ria
I am trying to close a text file by name that was opened in excel but unable to do so. Here is scenario:
I have a workbook A open that has button which triggers macro/vba select and open text file in excel.
Get data from opened text file and then close it without saving. Close by name, not by file number. I have a code that is working fine if I close text file by file number, but I am unable to close it by name. Reason to close it by name is, if by chance I have 2 workbooks open (e.g. workbook A, related to text file. And workbook B, not related to A or text file). If I close text file by file number than it closes workbook B and keeps open text file (opened in excel). Here is my code:
Code:
Sub FLDImportDataDaily()
'
' FLD Import Data Macro
' Macro recorded 27/12/2014 by Dani
'
Dim strFileToOpenSr As String, i As Integer, strFldName As String
'Dim FileNum As Integer
'Call delete data macro
FLDDeleteDataD
'FileNum = FreeFile
strFileToOpenSr = Application.GetOpenFilename _
(Title:="Please choose a file to open", _
FileFilter:="Text Files *.txt* (*.txt*),")
'===============
'FileNum = FreeFile
'Open strFileToOpenSr For Input As FileNum
'======================
'strFldName = ActiveWorkbook.Sheets(1).Range("M1").Value
'
'ChDir "C:\Users\Dani\Desktop\MyDeskTop\NewPC\JM_Hurst\RDATA"
Workbooks.OpenText Filename:=strFileToOpenSr, Origin:= _
xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
, ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=False, Comma:=True _
, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 4), Array(2, 1), Array _
(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:= _
True
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("JHurst.xls").Activate
Range("A4").Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("A4").Select
'Open strFileToOpenSr For Input As FileNum
'Windows(strFileToOpenSr).Activate
'ActiveWindow.Close
'Windows(strFileToOpenSr).Close
'Workbooks(strFileToOpenSr).Close
Application.DisplayAlerts = False
Workbooks(2).Close
'Workbooks(strFileToOpenSr).Activate
'Activeworbook.Close
'Close strFileToOpenSr
'Close FileNum
Application.DisplayAlerts = True
'Workbooks.Close Filename:=strFileToOpenSr
'Workbooks(strFileToOpenSr).Close
'txtWB.Close
'Windows(strFileToOpenSr).Activate
'ActiveWindow.Close
'Application.DisplayAlerts = False
End Sub
Any help would be appreciated.
Thanks,
Ria