Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Formating? 1

Status
Not open for further replies.

FM1

Structural
Dec 16, 2001
67
I just posted this question on the MS VB forum.
how could this be done through VBA in Excel? Or, can it be done?




"I am very new to VB and, am trying my hand at VB6.
I think!, I know how to creat a text file and have a string written to it.
What I am trying to do is have a column header, say, starting at column 15 of the second line and have the header field limitted to 10 spaces. have another header at column 30 .....etc. and have the coresponding values written below each header.

I will appreciate a sample code or name of a reference where to look this up."

Thank you
 
Replies continue below

Recommended for you

It has been a long time since I have done any text file programming. Look at the tab statement, I think this is what you want. There is also an example in help.

SA
 
FM1,
this is a rude example. The idea is to build a line of data and print to file.

HTH

LF


Code:
Public Sub FmtText()
Dim Ztring As String, sLine As String
Dim n1 As String, n2 As String, n3 As String, n4 As String

Ztring = Space(80)                                 ' page width 80 chars
Open "C:\TextFile.txt" For Output As 1

Print #1, vbCr                                      ' blank line
sLine = InsertAt("Title_Line @ Col34", 34, Ztring)  ' title
sLine = InsertAt(Date, 80 - Len(Date), sLine)
Print #1, sLine: Print #1, vbCr

sLine = InsertAt("C01 @ Col8", 8, Ztring)           ' column headers
sLine = InsertAt("C02 @ Col22", 22, sLine)
sLine = InsertAt("C03 @ Col44", 44, sLine)
sLine = InsertAt("C04 @ Col65", 65, sLine)
Print #1, sLine: Print #1, vbCr

For i% = 1 To 50                               ' say writing 50 lines...
    n1 = "Line #" & Format(i%, "00")
    n2 = Format(((500 * Rnd) + 0.001), "000.00000")
    n3 = Format(((50 * Rnd) + 0.001), "00.00000")
    n4 = Format(((5 * Rnd) + 0.001), "#.0000000")
    
    sLine = InsertAt(n1, 17 - Len(n1), Ztring)
    sLine = InsertAt(n2, 32 - Len(n2), sLine)
    sLine = InsertAt(n3, 54 - Len(n3), sLine)
    sLine = InsertAt(n4, 75 - Len(n4), sLine)
    Print #1, sLine                                   ' line of data...
Next

Close 1
End Sub


Public Function InsertAt(What As String, At As Integer, Where As String)
Dim l2 As String, l3 As String
  l0 = Len(Where)
  l1 = Len(What)
  l2 = Left(Where, At - 1)
  l3 = Right(Where, l0 - (l1 + At) + 1)
  InsertAt = l2 & What & l3
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor