Text import and transpose - pretty difficult

M

Mohan

Hi
I have a text file (tabe delimitted) that gives tha cash flow of intial
investment. This looks like:
ValDate PoolNo CF_date CF_Amt
08/01/2007 12345 01/01/2007 100.00
08/01/2007 12345 02/01/2007 200.00
08/01/2007 12346 01/01/2007 100.00
08/01/2007 12346 02/01/2007 200.00
08/01/2007 12346 03/01/2007 250.00
08/01/2007 12346 04/01/2007 175.00

I am using the follwoing code to import:

Public Sub ImportTextFile(FName As String, Sep As String)

Dim RowNdx As Long
Dim ColNdx As Integer
Dim TempVal As Variant
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Integer
Dim SaveColNdx As Integer

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1

End Sub

This macro basically imports the file in the same row/column format. I want
to transpose the result in excel to look like :
Val Date Pool # CF_date1 CF_Amt1 CF_date2 CF_Amt2 ....
and so on for as many as cashflows given for the same pool no. (so its not a
set number of columns. If there are two rows in the text file for one pool
number (see sample pool no 12345 above) it should look like
Val Date Pool # CF_date1 CF_Amt1 CF_date2 CF_Amt2
08/01/2007 12345 01/01/2007 100.00 02/01/2007 200.00

If there are 10 rows for the same pool no, then I get the ValDate, Pool# and
20 columns for the CF_date (1 to 10) and CF_amt (1 to 10) - All together 22
columns

How can I modify this code to achive this? or any other suggestions...

total cash flows will not exceed 60 months. I dont have to worry about
exceeding the column limitation in Excel 2003.

Thanks
Mohan
 
M

Mohan

No.. they are not sorted.
But I could this data into Excel in the same row/column format, sort them
and then copy to another worksheet within the same workbook and this time I
need to transform the cash flow date and amoutn into columns instead of rows.
 
M

Mohan

However, all pool numbers will be together. Not necessarily sorted in any
order.
same pool # will not be scattered in the file. so I could look at the next
record (or previous record) to see if the pool # changed (if thats what you
are thinking of doing...)
 
P

p45cal

See if this does it for you. First the test routine which calls the import
sub:

Sub test()
ImportTextFile "cashflow.txt", Chr(9)
End Sub

You say tab delimited, which is what Chr(9) is doing there.
Now the main bit (if you want headers right across the top, say so):

Public Sub ImportTextFile(FName As String, Sep As String)
Dim RowNdx As Long
Dim WholeLine As String
Dim ColNdx As Integer
Dim b As Range
Dim HeadersDone As Boolean
Application.ScreenUpdating = False
'On Error GoTo EndMacro:

ColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Open FName For Input Access Read As #1

While Not EOF(1)
Line Input #1, WholeLine
a = Split(WholeLine, Chr(9))
If a(1) = Cells(RowNdx, ColNdx + 1) Then
Set b = Cells(RowNdx, ColNdx).End(xlToRight)
b.Offset(, 1) = a(2): b.Offset(, 2) = a(3)
Else
If HeadersDone Then RowNdx = RowNdx + 1
Cells(RowNdx, ColNdx).Resize(, 4) = a
HeadersDone = True
End If
Wend

EndMacro:
On Error GoTo 0
Application.ScreenUpdating = True
Close #1
End Sub
 
P

p45cal

I wish there was a way to etract posts. It's disappointing not to get a
response from the opriginal poster after you've clearly spent significant
time and effort solving THEIR problem.
 

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

Top