Splitting on Newline?

Err

Active Member
Joined
Nov 16, 2006
Messages
274
Here is the piece of code that is bugging me.

Code:
Public Function Convert_File(from_file As String) As Variant
    
    Dim fso As Object
    Dim fil As Object
    Dim txt As Object
    Dim strtxt As String
    Dim stringArray() As String
    Dim string_arrayPiece() As Variant
    Dim string_arrayE As Variant 'array element
    Dim repost_String As String
    Dim i As Integer

    LastNonEmpty = -1
    repost_String = ""
    Dim n As Long
    
    
    'filesyestem object that we need to manage files
    Set fso = CreateObject("Scripting.FileSystemObject")

    'File that we like to open and read
    Set fil = fso.GetFile(from_file)
    'Opening file as a text string
    Set txt = fil.OpenAsTextStream(1)
    strtxt = txt.ReadAll
    
    'closes TextStream and free the file as we don't need it anymore
    txt.Close
    
    stringArray() = Split(strtxt, vbCrLf)
    
    'get size
    ReDim string_arrayPiece(UBound(stringArray()), 1)
    
    Convert_File = stringArray()



End Function

Here is a sample text file:

Code:
WHEREAS, the Landlord has entered into a Landlord Services Agreement with Rent_insanity.com pursuant to
which Landlord has agreed to accept rent payments from Rent_insanity.com and agreed to pay Rent_insanity.com a fee;
WHEREAS, Rent_insanity.com desires to provide its default reduction services on the Client's behalf, on the
terms set forth herein; and
NOW THEREFORE, in consideration of the mutual covenants expressed herin and for the
good and valuable consideration, the revept and sufficiency of which are hereby acknowledged, and
intending to be legally bound, the parties agree as follows:
1.1. Agency Appointment; Services. Client appoints Rent_insanity.com as its agent to reduce Rent default to its
Landlord in accordance with the terms of this Agreement. Rent_insanity.com will receive funds transferred to
Rent_insanity.com at the direction of and on behalf of the Client and on the date of each month set forth on the
signature page hereto (the 'Rent Transfer Date'), Rent_insanity.com, will transfer to the Landlord, on behalf of the
Client, such funds held by Rent_insanity.com equal to the monthly rent due and payable (the 'Rent Payment')
under the Lease and specified on the Signature Page hereto, subject to the terms and conditions of
this Agreement (the 'Services').


In typical word fashion... each paragraph just drones on until there is an actual carriage return. I would like for this function to split up the entire string into it's individual substrings and hold them as an array... so that I can put them back together again at my leisure.

The problem is that I'm having trouble telling the split function to look for the end of line...
 
Just made a few edits above ... noticed we hadn't closed the textstream in the last procedure ...
ξ
 
Upvote 0

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.
Surely, because as you say: "each paragraph just drones on until there is an actual carriage return", therte are no carriage returns, newlines, linefeeds, whatever, in each paragraph. So I'd have thought you have to add your own.
Could you post a sample file of the kind you want excel to examine/open somewhere on the interweb - say box.net amongst many?

I've written a little sub to split the lines with whatever character you want, at spaces up to a max of n characters, but don't want to post it until I'm sure it'll be relevant and work. (One thing I forsee is that in the sample text you included, there is at leastone line which is shorter than the others and I'd like to see it to know how to deal with it.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,947
Latest member
Gerry_F

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