Text File Specifications

Victor Moss

Board Regular
Joined
May 3, 2014
Messages
90
Hi guys,

I've been streaming Excel Data to a Text files for years, the Text file consists of a Header Record, Detail Records and a Trailer Record, it's been working great, I've now had to start adding Contra Records to any variable number of transactions, my problem is I don't know how to Loop the them in between the Detail Records.

*WD50TEST20210514YTEST+61001LIVE2WD50T1425502
2WD504700100PHA00312254228721461783MS HAIBOSALARY
2WD504700100MHL01113618859101461783MS WENASALARY
2WD50510010LIM005122497878431501936NS BLESSINGSALARY


As you can see I capture the Trailer Record on Row1 between Columns L & O, but Like I said, I need to capture Contra Records which come before Trailer records and could be an unknown number of Contra Records.

Here is my code so far working with a Buffer Function:

Function BUFFER(ByVal Value As String, Length As Long, Optional Front As Boolean = False, Optional BuffChar As String = " ") As String
If Len(Value) > Length Then Err.Raise 2001, "BUFFER UDF", "Value exceeds length requirements"
If Len(Value) = Length Then
BUFFER = Value
Exit Function
End If
If Front Then
Value = BuffChar & Value
Else
Value = Value & BuffChar
End If
BUFFER = BUFFER(Value, Length, Front, BuffChar)
End Function



Open FPath & fName For Output As File

rowno = 0

rowno = rowno + 1
Range("A" & rowno).Select

If Selection.Value = "" Then
done = True

Else

CountRec = CountRec + 1

' Header Spec
Line = BUFFER(Range("A" & rowno).Text, 1)
Line = Line & BUFFER(Range("B" & rowno).Text, 4)
Line = Line & BUFFER(Range("C" & rowno).Text, 40)
Line = Line & BUFFER(Range("D" & rowno).Text, 8)
Line = Line & BUFFER(Range("E" & rowno).Text, 1) & " "
Line = Line & BUFFER(Range("F" & rowno).Text, 15, False)
Line = Line & BUFFER(Range("G" & rowno).Text, 1)
Line = Line & BUFFER(Range("H" & rowno).Text, 2)
Line = Line & BUFFER(Range("I" & rowno).Text, 1) & " "
Line = Line & BUFFER(Range("J" & rowno).Text, 2)
Line = Line & BUFFER(Range("K" & rowno).Text, 4)
Print #File, Line
End If

CountRec = 0
TAmount = 0

rowno = 1

Do Until done

rowno = rowno + 1
Range("B" & rowno).Select

If Selection.Value = "" Then
done = True

Exit Do
Else
'check if the value is zero
Range("G" & rowno).Select
If Val(Selection.Value) = 0 Then 'NOTHING

Else

CountRec = CountRec + 1

' Detail Spec
Line = BUFFER(Range("A" & rowno).Text, 1)
Line = Line & BUFFER(Range("B" & rowno).Text, 4)
Line = Line & BUFFER(Range("C" & rowno).Text, 6, True, "0")
Line = Line & BUFFER(Range("D" & rowno).Text, 7, True, "0")
Line = Line & BUFFER(Range("E" & rowno).Text, 19, True, "0") & " "
Line = Line & BUFFER(Range("F" & rowno).Text, 1)
Line = Line & BUFFER(Range("G" & rowno).Text, 11, True, "0")
Line = Line & BUFFER(Range("H" & rowno).Text, 20) & " "
Line = Line & BUFFER(Range("I" & rowno).Text, 15, False)

Print #File, Line
End If
End If

Loop

CountRec = 0
TAmount = 0

rowno = 0

rowno = rowno + 1
Range("L" & rowno).Select

CountRec = CountRec + 1

' Trailer spec
Line = BUFFER(Range("L" & rowno).Text, 1)
Line = Line & BUFFER(Range("M" & rowno).Text, 4)
Line = Line & BUFFER(Range("N" & rowno).Text, 1) & " "
Line = Line & BUFFER(Range("O" & rowno).Text, 13, True, "0")

Print #File, Line

Close File



If anyone can help please?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,214,986
Messages
6,122,611
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top