Cell containing multiple lines of text - rebuilding the same structure with VBA - something is not working

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
I want to concatenate the lines of a cell, but somehow my loop is not working and gives a weird result. (picture below)

Could somebody help me out what am I doing wrong? I have tried so hard to find my mistake but seems I can't.

Thank you very much in advance.

VBA Code:
Public Function EveryLineToNewLine(snippet As String) As String

Dim i As Integer
Dim output As String
Dim elozo As Integer
Dim debugg As String

i = 1
output = ""
previousPosition = 1


        'I have 5 lines, i want to run the loop 4 times (so 4 "new line" will be inserted,
        'resulting in 5 lines
        Do While i < 5
          
            'we look for new line --> starting at 1, in the cell passed as argument, and looking for new line character
            chrPoz = InStr(previousPosition, snippet, Chr(10), vbBinaryCompare)
          
            'then we concatenate it to the end of our string and we insert a new line
            output = output & Mid(snippet, previousPosition, chrPoz) & Chr(10)
          
            'then we want to start the search next time after the current new line +1
            previousPosition = chrPoz + 1
          
            'increase loop count
            i = i + 1
          
            'and notice character indexes to see the number of loops and found new lines indexes
        debugg = debugg & chrPoz & ","
      
        Loop

    'adding last line (will return all character after last new line character)
      output = output & Mid(snippet, previousPosition)

EveryLineToNewLine = output & "" & "debug:" & debugg


End Function

1599249828712.png
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
It should be
Rich (BB code):
            output = output & Mid(snippet, previousPosition, chrPoz - previousPosition) & Chr(10)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,064
Members
448,941
Latest member
AlphaRino

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