Rayne
asked on
Combine First Two rows in text file
Hi,
I have a text file where I want to combine the first and second row into one row. How do I do it using the
Do Until objFile.AtEndOfStream
Loop
Thanks
SampleData.txt
what-is-needed.png
I have a text file where I want to combine the first and second row into one row. How do I do it using the
Do Until objFile.AtEndOfStream
Loop
Thanks
SampleData.txt
what-is-needed.png
Is it only 3 columns?
Also, you want to output on the same sheet with original data and new data as in the picture?
With the formats (line and color, etc...) ??
Also, you want to output on the same sheet with original data and new data as in the picture?
With the formats (line and color, etc...) ??
You can use code like this:
If you like to do it in text file, show your code
For i = 1 To 3
Cells(1, i) = Cells(1, i) & " " & Cells(2, i)
Next i
Rows(2).Delete
Correct row numbers if it is not 1 and 2If you like to do it in text file, show your code
Run the MergeFirstTwoRows macro.
Sub MergeFirstTwoRows()
Dim FF As Integer
Dim lngNextRow As Long
Dim strline As String
Dim bMerged As Boolean
FF = FreeFile
' Change this line
Open "C:\Solutions\SampleData.txt" For Input As #FF
Do While Not EOF(FF)
Line Input #FF, strline
Select Case lngNextRow
Case 0, Is > 1
If Not bMerged Then
lngNextRow = lngNextRow + 1
End If
bMerged = False
WriteData strline, lngNextRow
Case Else
WriteData strline, lngNextRow, True
lngNextRow = lngNextRow + 1
bMerged = True
End Select
Loop
Close
End Sub
Private Sub WriteData(strline As String, lngRow As Long, Optional bMerge As Boolean = False)
Dim strParts() As String
Dim intPart As Integer
strParts = Split(strline, vbTab)
With ActiveSheet
For intPart = 0 To UBound(strParts)
If Not bMerge Then
.Cells(lngRow, intPart + 1) = strParts(intPart)
Else
.Cells(lngRow, intPart + 1) = .Cells(lngRow, intPart + 1) & " " & strParts(intPart)
End If
Next
End With
End Sub
ASKER
Hi Martin,
I tried your code on the sample file I have attached previously and it didn't work
I tried your code on the sample file I have attached previously and it didn't work
ASKER
Martin, your code is not working or showing the final result. Have you tested it on the sampledata.txt?
ASKER
Hi Tuvi,
for now, its only 3 columns but that can change in the long-term
It can be the same file that has the intended change or new file, it doesn't matter
for now, its only 3 columns but that can change in the long-term
It can be the same file that has the intended change or new file, it doesn't matter
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, everyone, for chipping in. you all are a lifesaver. Thank you
You’re welcome and I’m glad I was able to help.
If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.
Marty - Microsoft MVP 2009 to 2017
Experts Exchange Most Valuable Expert (MVE) 2015, 2017
Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
Experts Exchange Top Expert VBA (current)
If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.
Marty - Microsoft MVP 2009 to 2017
Experts Exchange Most Valuable Expert (MVE) 2015, 2017
Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
Experts Exchange Top Expert VBA (current)
ASKER