Wanting to display a text file in particular way

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello
My Textfile Age.Txt is in following manner where data is copied from other sources
Name Tom
Age 13

Name John
Age 15

Name Jack
16
Age

Name Michael
Age
12

Name Martin
Age 33

Name George
43
Age
Would like to display in the below following manner
Name Tom
Age 13

Name John
Age 15

Name Jack
Age 16

Name Michael
Age 12

Name Martin
Age 33

Name George
Age 43
These are just 6 records as mentioned above but there are more than 200
I am irritated to correct each record as per the output required
ie if one observes the numerical value of Age comes in previous line before word Age or it comes in next line after word Age. Would like to have the numerical value of age in the line of word Age

Any ideas for a suitable code

SamD
166
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
fPath is path to Age.Txt - please amend path before running the code
The code creates a new file named Result.Txt (in the same folder as Age.Txt)

VBA Code:
Sub CreateTextFile()
    Dim F1 As String, F2 As String, NewText As String, OldText As String
    Const fPath = "C:\Test\Folder\Subfolder"      'AMEND
    
    F1 = fPath & "\" & "Age.txt"
    F2 = fPath & "\" & "Result.txt"
    Open F1 For Input As #1
    Open F2 For Output As #2

    Do Until EOF(1)
        Line Input #1, OldText
        If OldText Like "Name*" Then NewText = OldText
        If OldText Like "Age*[0-9]*" Then NewText = OldText
        If IsNumeric(OldText) Then NewText = "Age " & OldText
        If Len(NewText) > 0 Then
            Print #2, NewText
            If NewText Like "Age*[0-9]*" Then Print #2, vbNullString
        End If
        NewText = ""
    Loop
    
    Close #1
    Close #2

End Sub
 
Upvote 0
Yongle Dear

Sorry for late reply

Thank you so much for your valuable input and worked perfectly.

What does this mean Like "Name*" and Like"Age*[0-9]*" and what made you opt to choose "Like" syntax.

2nd Question
Will it be possible to get the above result as per your reply #2 using fso. OpenTextFile(FileFullName). If yes then how.

I am trying to understand and explore various options

Guidance shall be appreciated

SamD
167
 
Upvote 0
What does this mean Like "Name*" and Like"Age*[0-9]*" and what made you opt to choose "Like" syntax.

WHY USE?
Like allows the use of wildcards and string patterns to be checked without being specific
- it is very flexible and reduces requirement for loops when testing for multiple characters
Read here


If OldText Like "Name*"
Means If OldText begins with this string "Name"

Could have included a space before *
If OldText Like "Name *"

Which would give same result as ...
If Left(OldText, 5) = "Name "

If OldText Like "Age*[0-9]*"
Means If OldText begins "Age" followed by any characters followed by at least one number
 
Upvote 0
2nd Question
Will it be possible to get the above result as per your reply #2 using fso. OpenTextFile(FileFullName). If yes then how.


VBA Code:
Sub CreateTextFileFSO()
    Dim oFSO As Object, oFS As Object
    Dim F1 As String, F2 As String, NewText As String, OldText As String
    Const fPath = "C:\Test\Folder\Subfolder"      'AMEND
   
    F1 = fPath & "\" & "Age.txt"
    F2 = fPath & "\" & "Result.txt"

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFS = oFSO.OpenTextFile(F1)
    Open F2 For Output As #1

    Do Until oFS.AtEndOfStream
        OldText = oFS.ReadLine
        If OldText Like "Name*" Then NewText = OldText
        If OldText Like "Age*[0-9]*" Then NewText = OldText
        If IsNumeric(OldText) Then NewText = "Age " & OldText
        If Len(NewText) > 0 Then
            Print #1, NewText
            If NewText Like "Age*[0-9]*" Then Print #1, vbNullString
        End If
        NewText = ""
    Loop
    Close #1
End Sub
 
Last edited:
Upvote 0
Solution
Thank you so much for your valuable input and worked perfectly

Perhaps you could mark that post as a Solution

How to do that is explained here (page down until you find Mark as Solution)
 
Upvote 0
Yongle

Mind Blowing Explanation. you have explained beautifully with link attached and excellent fso_Open File solution.

It seems fso. OpenFile name will be always coded to read a mostly a text file or data file. and not for output as what was desired.

Thankx a Tonne ?

SamD
168
 
Upvote 0

Forum statistics

Threads
1,214,540
Messages
6,120,107
Members
448,945
Latest member
Vmanchoppy

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