×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Text File Formating?

Text File Formating?

Text File Formating?

(OP)
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

RE: Text File Formating?

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

RE: Text File Formating?

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources