View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Lonnie M. Lonnie M. is offline
external usenet poster
 
Posts: 184
Default how to lmport a text file and save it as an excel file using vb s.

I'm a Newbie too,
but I think you need to run text-to-columns after you open the file.
When import a text file it all gets dumped into the first column.
Then you can format, autofit, and save.

See the examples below (Text-to-columns):
Workbooks.Open FileName:=Fname

'Method I
Set mySheet = ActiveSheet
myShtName = ActiveSheet.Name
R = mySheet.UsedRange.Rows.Count
mySheet.Range(Cells(1, 1), Cells(R, 1)).Select
Application.Dialogs(xlDialogTextToColumns).Show
Columns.AutoFit

'Method II
Application.Dialogs(xlDialogOpen).Show Arg1:=Fname
Set mySheet = ActiveSheet
myShtName = ActiveSheet.Name
R = mySheet.UsedRange.Rows.Count
Columns.AutoFit

See the examples below (Saving file as 'xls'):
In VBA the code for saving the file would look like this, where Fname
is a string; I.E: Fname = ThisWorkbook.Path & Application.PathSeparator
& "MyFile.xls":
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=Fname, FileFormat:=xlNormal
Application.DisplayAlerts = True

HTH, Lonnie