Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 65
Default Code to import from txt to excel

Any help, highly appreciated:

I am trying to get a macro to import a text file in a table format into excel
I have the following code but this code does not give me the format properly:

Sub importtext()
x = 1
Open "c:\trial.txt" For Input As #1
Do While Not EOF(1)
Input #1, Line
Worksheets("sheet1").Cells(x, 1).Value = Line
x = x + 1
Loop
Close #1
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 611
Default Code to import from txt to excel

N.F.:

You might better set up an import query. Data - Import external data - Import data. It
will give you a Select Data Source dialog. Navigate to the folder of your text file, change
the file type box to that of your text file (*.txt, *.csv, whatever), and select it. It
will then start the Text Import Wizard, where you can tell it if it's delimited or fixed,
and all kinds of stuff. When you've done, the data will be in the sheet. Then when you
want it to read the file again (into the same sheet, replacing the old data), right click
the area in the sheet, and choose Refresh. You can record a macro for doing the refresh if
you wish. It takes the grunt work out of importing data very nicely.
--
Earl Kiosterud
www.smokeylake.com

Note: Top-posting has been the norm here.
Some folks prefer bottom-posting.
But if you bottom-post to a reply that's
already top-posted, the thread gets messy.
When in Rome...
-----------------------------------------------------------------------
"N.F" wrote in message
...
Any help, highly appreciated:

I am trying to get a macro to import a text file in a table format into excel
I have the following code but this code does not give me the format properly:

Sub importtext()
x = 1
Open "c:\trial.txt" For Input As #1
Do While Not EOF(1)
Input #1, Line
Worksheets("sheet1").Cells(x, 1).Value = Line
x = x + 1
Loop
Close #1
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 65
Default Code to import from txt to excel

Thank you Earl

"Earl Kiosterud" wrote:

N.F.:

You might better set up an import query. Data - Import external data - Import data. It
will give you a Select Data Source dialog. Navigate to the folder of your text file, change
the file type box to that of your text file (*.txt, *.csv, whatever), and select it. It
will then start the Text Import Wizard, where you can tell it if it's delimited or fixed,
and all kinds of stuff. When you've done, the data will be in the sheet. Then when you
want it to read the file again (into the same sheet, replacing the old data), right click
the area in the sheet, and choose Refresh. You can record a macro for doing the refresh if
you wish. It takes the grunt work out of importing data very nicely.
--
Earl Kiosterud
www.smokeylake.com

Note: Top-posting has been the norm here.
Some folks prefer bottom-posting.
But if you bottom-post to a reply that's
already top-posted, the thread gets messy.
When in Rome...
-----------------------------------------------------------------------
"N.F" wrote in message
...
Any help, highly appreciated:

I am trying to get a macro to import a text file in a table format into excel
I have the following code but this code does not give me the format properly:

Sub importtext()
x = 1
Open "c:\trial.txt" For Input As #1
Do While Not EOF(1)
Input #1, Line
Worksheets("sheet1").Cells(x, 1).Value = Line
x = x + 1
Loop
Close #1
End Sub





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Import to Excel Issue with Excel 2007 and Excel 2003 on same Melsh Excel Discussion (Misc queries) 0 August 1st 07 09:32 PM
Convert a Number Code to a Text Code Traye Excel Discussion (Misc queries) 3 April 6th 07 09:54 PM
Trying to write vba code to import another spreadsheet... joep3 Excel Discussion (Misc queries) 3 September 6th 06 06:50 PM
Import a Code to a paragraph using a formula or macro! Manos Excel Worksheet Functions 3 February 11th 06 07:48 AM
copying vba code to a standard code module 1vagrowr Excel Discussion (Misc queries) 2 November 23rd 05 04:00 PM


All times are GMT +1. The time now is 04:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"