Following is the code I've used for importing a Text File:
1. It imports Text File (keeping data in one column).
2. Changes the font to Courier (Fixed Pitch font) so that print looks similar to Text File.
3. Adds page breaks to separate Data Chunks for easier read (Hard Copy)
Here it is:
[VBA]'This opens selected 'Text' file in specified 'Excel' format
GetOpenFile = Application.GetOpenFilename
Workbooks.OpenText Filename:= _
GetOpenFile, Origin:=437, StartRow:=1, DataType:=xlDelimited, TrailingMinusNumbers:=True
'This copies and saves data in MTO file.
ActiveSheet.Select
MTOfileLoc = Workbooks("SETPAGE").Sheets.Count
ActiveSheet.Copy After:=Workbooks("SETPAGE.xls").Sheets(MTOfileLoc)
SheetNo = ActiveWorkbook.Sheets.Count
Worksheets(SheetNo).Select
SheetName = Workbooks("SETPAGE").Sheets(SheetNo).Name
Windows(SheetName).Activate
ActiveWindow.Close
Sheets(SheetName).Columns("A:A").Select
With Selection.Font
.Name = "Courier"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Sheets(SheetName).Columns("A:A").EntireColumn.AutoFit
'VBAX: Thank you.
Dim c As Range
Dim FirstAddress As String
Dim Search As String
Dim Prompt As String
Dim Title As String
Prompt = "What do you want to search for?"
Title = "Search Term Input"
Search = InputBox(Prompt, Title)
If Search = "" Then
Exit Sub
End If
With ActiveSheet.UsedRange
Set c = .Find(What:=Search, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
If c.Address = FirstAddress Then
Set c = .FindNext(c)
Else
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=c
Set c = .FindNext(c)
End If
Loop While Not c Is Nothing And c.Address <> FirstAddress
End If
End With
'VBAX: Thank you
Sheets(SheetName).Range("A1").Select[/VBA]
This code runs absolutely fine if:
1. Excel instance is fresh and no other Text File is imported.
2. If a Text File is imported (especially using ':' as delimiter) then this code goes haywire.
3. It imitates importing i.e. delimits ':' which I do not want.
4. Closing and restarting is helping me but that is not good solution.
I want "Excel" to forget its previous import and I've reached a point where I just can't think of an idea Please