Excel VB to print fix length txt file-no decimal point

RockinBill

New Member
Joined
Apr 29, 2009
Messages
1
My first time posting. I need help to create a text file from Excel that can be imported into my Accounting system which requires a fixed length records. I am having trouble outputting the number fields in the correct format. These number fields must be 12 positions of which 2 are decimal places but the output can not contain a decimal point. The source data will have a zero, one decimal place or many decimal places. The output (printed) field must be right justified and padded with spaces.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
0 must end up 1 or more zeros mine moves 2 spaces to right 510.00 must end up 51000 mine moves 3 to the right
1610.5 must end up 161050 mine moves 1 space to right
1001.517 must end up 100152 100151.7 mine moves 1 and adds decimal
<o:p></o:p>
Here is my code so far. I inserted a ' and line number to make it easer to view here. Lines 12 and 13 are the ones not working correctly.
<o:p></o:p>
'1 Sub MakeFixedWidth()
'2 Dim MyStr As String, PageName As String, FirstRow As Integer, LastRow As Integer, MyRow As Integer
'3 PageName = "C:\JE Upload" & Format(Time, "HHMMSS") & ".txt" 'location and name of saved file
'4 FirstRow = Range("H1").Value 'the range of the table to be exported
'5 LastRow = FirstRow + Range("H2").Value - 1
'6 Open PageName For Output As #2
'7 Print #2, "01" & Format(Date, "yyyymmdd") & "GL TRANSACTIONS UPLOADED " & Format(Date, "yyyymmdd") & " " & Format(Time, "HHMMSS") '01 record RT-2, Create date, FILE ID LEN(35)
'8 Print #2, "05" & "BATCH " & Range("E11") & String(24 - Len(Range("E11")), " ") & Format(Date, "yyyymmdd") & Range("g10") & Range("a5") 'batch header rt-2,Btch ID-30,Btch Dt, post to dt,journal name
'9 For MyRow = FirstRow To LastRow 'loop through each row of the table
'10 MyStr = ""
'11 MyStr = "10" & Cells(MyRow, 1).Value & String(25 - Len(Cells(MyRow, 1).Value), " ") 'Rec type + GL account number
'12 MyStr = MyStr & String(12 - Len(Cells(MyRow, 5).Value numberformat=###.00), " ") & Cells(MyRow, 5).Value * 100 'Debit amount
'13 MyStr = MyStr & String(12 - Len(Cells(MyRow, 6).Value), " ") & Cells(MyRow, 6).Value * 100 'Credit amount
'14 Print #2, MyStr
'15 Next
'16 Print #2, "99" ' MyTrailer
'17 Close #2
'18 Sheets("JE Upload").Range("F2").ClearContents ' note that this row expects the worksheet to be named JE Upload
'19 Sheets("JE Upload").Hyperlinks.Add Range("F2"), PageName
'20 End Sub<o:p></o:p>
<o:p></o:p>
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,215,731
Messages
6,126,532
Members
449,316
Latest member
sravya

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