View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_1317_] Rick Rothstein \(MVP - VB\)[_1317_] is offline
external usenet poster
 
Posts: 1
Default Importing ASCII text into worksheet

I had to make some assumptions about your data. You said the first field was
9 characters wide, but that is not what you posted for those starting with
*, so I assumed what you posted is what is actually in the file (if not, the
code should still work). Also, I am assuming that, as shown in your example,
**each** line starting with GRID* is followed by **one, and only one** line
starting with * (if this is not the case, then the code will **not** work
correctly for you). Change the filename in the Open statement to your text
file's path and name...

Sub LoadDateValues()
Dim X As Long
Dim FileNum As Long
Dim RowNum As Long
Dim TotalFile As String
Dim Lines() As String
FileNum = FreeFile
Open "C:\TEMP\TestData.txt" For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
TotalFile = Mid$(TotalFile, InStr(TotalFile, "GRID*"))
Lines = Split(TotalFile, vbNewLine)
RowNum = 1
With Worksheets("Sheet1")
For X = 0 To UBound(Lines)
If Left$(Lines(X), 4) = "GRID" Then
.Cells(RowNum, "A").Value = Trim$(Mid$(Lines(X), 17, 16))
.Cells(RowNum, "B").Value = Trim$(Mid$(Lines(X), 41, 16))
.Cells(RowNum, "C").Value = Trim$(Mid$(Lines(X), 57, 16))
Else
.Cells(RowNum, "D").Value = Trim$(Left$(Trim$(Mid$(Lines(X), 2)),
16))
RowNum = RowNum + 1
End If
Next
End With
End Sub

Rick

"nutrition" wrote in message
...
I need a program that will read information from an ASCII file, which data
is
organized in fixed width. The width of the first column is 8 characters
long
and the columns that follow are in width of 16 characters long.

For rows that start with GRID*, the 2nd column needs to be imported into a
worksheet in Excel in column A. For the same row, the data in the 4th
column
needs to be imported into the same worksheet into column B and the data in
5th column needs to be imported into the same worksheet in column C.

For rows that start with *, the 2nd column needs to be imported into the
same row above in column D.


The following is some sample data:


$
$ --------------- begin exec section ---------------------------
$
CEND
$
$
$ ---------------- end exec section ---------------------------
$ --------------- begin case control ---------------------------
$
TITLE =
ECHO = NONE
OUTPUT
METHOD = 10
SPC = 1
$
$
$ ---------------- end case control ---------------------------
$ --------------- begin bulk data ---------------------------
$
BEGIN BULK
PARAM,
PARAM,
E
GRID* 11948 02.547415380E+0035.913292032E+002+
* 1.392468901E+003 0 0
GRID* 11949 02.568299386E+0035.823013553E+002+
* 1.404233550E+003 0 0
GRID* 11950 02.560183765E+0036.110349623E+002+
* 1.393538123E+003 0 0
GRID* 11951 02.585286928E+0036.047367958E+002+
* 1.406622591E+003 0 0
GRID* 11952 02.604819484E+0034.976212049E+002+
* 1.437870452E+003 0 0
GRID* 11953 02.587617792E+0035.171256767E+002+
* 1.426662596E+003 0 0
GRID* 11954 02.579635566E+0034.921914698E+002+
* 1.427609606E+003 0 0
GRID* 11955 02.608029258E+0035.197200252E+002+
* 1.435260482E+003 0 0
GRID* 11956 02.509450618E+0031.571528462E+002+
* 1.430737695E+003 0 0
GRID* 11957 02.511725472E+0031.316694151E+002+
* 1.432848509E+003 0 0

The results should be like this:

Column A Column B Column C Column D
11948 2.547415380E+003 5.913292032E+002 1.392468901E+003
11949 2.568299386E+003 5.823013553E+002 1.393538123E+003
etc...


Can anyone help?