A Macro that gets data from an ASCII file




I would like to write a Macro that does the following:

1. Opens an ASCII file (e.g. a *.txt or *.raw file)
2. Copies the data from the ASCII file into the Excel file.
Every line from the ASCII line should be inserted into a new row
3. closes the file

Could someone tell me how to do this?



Go to TOOLS|Macro
Start recording a macro
Choose name

Choode File|Open
Select your file
Make your choices in the import wizard

Stop recording your macro

Select your macro and click EDIT

You have your macro
Now you can adapt it

Chip Pearson

Try some code like the following:

Sub AAA()
Dim FName As Variant
Dim FNum As Integer
Dim LineRead As String
Dim R As Range
Set R = ActiveSheet.Range("A1") '<<< CHANGE START CELL
FName = Application.GetOpenFilename( _
"Text Files (*.txt;*.raw),*.txt;*.raw")
If FName = False Then
' no file selected
Exit Sub
End If
FNum = FreeFile()
Open FName For Input Access Read As #FNum
Do Until EOF(FNum)
Line Input #FNum, LineRead
R.Value = LineRead
Set R = R(2, 1)
Close #FNum
End Sub

Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
(email on web site)

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question