using instr() to break up long strings of text

davers5

Active Member
Joined
Feb 20, 2002
Messages
255
I have many lines of text that need to be broken up into pieces. Basically, the need to be broken up if there is more than one space between words. I was going to do this by searching for two spaces using the instr function and then looking for the next text character. Is there a way, using the instr function to find the first non-space character in a string? Maybe there's a wildcard or something? Thanks for the help,

Dave
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
On 2002-02-21 09:34, davers5 wrote:
I have many lines of text that need to be broken up into pieces. Basically, the need to be broken up if there is more than one space between words. I was going to do this by searching for two spaces using the instr function and then looking for the next text character. Is there a way, using the instr function to find the first non-space character in a string? Maybe there's a wildcard or something? Thanks for the help,

Dave

Will there only be 2 spaces between the words that need to be separated? Or could there also be 3 or more spaces?
 
Upvote 0
There are two or more spaces between text characters that need to be seperated. Text segments with only one space between them need to stay together. Here is a sample:
"Account Type Claude Killey $65" would need to be split to "Account Type, Claude Killey, $65"
 
Upvote 0
If all of the data you want on the rows are in roughly the same order in columns, then you could use the Data/Text to columns function.

Just an idea.
 
Upvote 0
Well, that example didn't look right when it was posted to the board. There were a bunch of spaces (two or more) between the word "type" and "Claude" and a bunch of spaces between "Killey" and "$65".
 
Upvote 0
On 2002-02-21 10:05, davers5 wrote:
There are two or more spaces between text characters that need to be seperated. Text segments with only one space between them need to stay together. Here is a sample:
"Account Type Claude Killey $65" would need to be split to "Account Type, Claude Killey, $65"

Here is a function that will split your string out for you (puts it into an array):

Code:
Option Explicit

Sub SplitOut()
    Dim strMain As String
    Dim astrSplit() As String
    Dim intI As Integer
    
    intI = 1
    ' Change the line below to point to your string.
    ' - you could also pass it in if you like.
    strMain = Range("A5")
    
    Do While InStr(Trim(strMain), "  ") > 0
        ReDim Preserve astrSplit(1 To intI)
        astrSplit(intI) = _
          Left(Trim(strMain), InStr(Trim(strMain), "  ") - 1)
        strMain = Mid(Trim(strMain), InStr(Trim(strMain), "  "))
        intI = intI + 1
    Loop

    ReDim Preserve astrSplit(1 To intI)
    astrSplit(intI) = Trim(strMain)

    For intI = 1 To UBound(astrSplit)
        Debug.Print astrSplit(intI)
    Next intI

End Sub

Hope this helps,

Russell
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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