Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / January 2008

Tip: Looking for answers? Try searching our database.

Split field

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dk - 28 Dec 2007 18:13 GMT
We would like to split a field which has sometimes 3,4,5, word in the field
the split should be as follows left word shall be copied in 1 cell, right
word in 1 cell ,all middle words together in  1 cell
Ron Rosenfeld - 28 Dec 2007 19:00 GMT
>We would like to split a field which has sometimes 3,4,5, word in the field
>the split should be as follows left word shall be copied in 1 cell, right
>word in 1 cell ,all middle words together in  1 cell

A1:    original string
B1:    =LEFT(TRIM(A1),FIND(" ",TRIM(A1))-1)

C1:   
=MID(TRIM(A1),LEN(B1)+2,
LEN(TRIM(A1))-(LEN(B1)+LEN(D1))-1)

D1:
=MID(TRIM(A1),FIND(CHAR(1),SUBSTITUTE(
TRIM(A1)," ",CHAR(1),LEN(TRIM(A1))-LEN(
SUBSTITUTE(TRIM(A1)," ","")))),255)
--ron
dk - 30 Dec 2007 03:39 GMT
thanks ok

how abou a fieild with only 2,3 words havin the first column everything
besides last word in 1 column last word in second column?

> We would like to split a field which has sometimes 3,4,5, word in the field
> the split should be as follows left word shall be copied in 1 cell, right
> word in 1 cell ,all middle words together in  1 cell
Ron Rosenfeld - 30 Dec 2007 12:56 GMT
>how abou a fieild with only 2,3 words havin the first column everything
>besides last word in 1 column last word in second column?

If there are only 2,3 words, do you want the last word in column 3 (lined up
with the last word if there are more than 3 words) or do you want the last word
in column 2?

What about if there is only one word?

What do you want to display if there are no words? -- <blank>?  <error
message>?
--ron
dk - 30 Dec 2007 15:36 GMT
only the last if  there is 1 it's should go in column 2 all others in column
1 bassically only the last in column 2, column 2 shall not be empty

> >how abou a fieild with only 2,3 words havin the first column everything
> >besides last word in 1 column last word in second column?
[quoted text clipped - 8 lines]
> message>?
> --ron
Ron Rosenfeld - 30 Dec 2007 21:51 GMT
>only the last if  there is 1 it's should go in column 2 all others in column
>1 bassically only the last in column 2, column 2 shall not be empty
[quoted text clipped - 11 lines]
>> message>?
>> --ron

If I understand you correctly (and you can check by looking at the comments at
the top of this VBA macro), then this should do what you want.

It could be done with formulas, but it would be exceedingly complex and time
consuming to devise.

The UDF could possibly be simplified, but I believe this solution will work.

To enter the UDF, <alt-F11> opens the VBEditor.  Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this UDF, select your range of cells.  <alt-F8> opens the Macro dialog
box.  Select the Macro, and <run>.

Let me know if this does what you need.

===================================================
Option Explicit
Sub SplitSpecial()
'splits multi-word string into adjacent
'   columns as follows
'1 word --> col2
'2 words --> col1 & col2
'3 words --> 1st 2 in col1; last in col2
'4+ words --> 1st in col1; last in col3; rest in col2
Dim c As Range
Dim re As Object, mc As Object, m As Object
Dim i As Long
Set re = CreateObject("vbscript.regexp")
re.Pattern = "^(\w+\b)?(\s*(.*?)\s*)(\b\w+$)"

For Each c In Selection
c.Offset(0, 1).Resize(1, 3).ClearContents
If re.test(c.Text) Then
   Set mc = re.Execute(c.Text)
       If mc(0).submatches.Count > 0 Then
           If InStr(1, mc(0).submatches(2), " ") = 0 Then
               c.Offset(0, 1).Value = Trim(mc(0).submatches(0) & _
                   " " & mc(0).submatches(2))
               c.Offset(0, 2).Value = mc(0).submatches(3)
           Else
               c.Offset(0, 1).Value = mc(0).submatches(0)
               c.Offset(0, 2).Value = mc(0).submatches(2)
               c.Offset(0, 3).Value = mc(0).submatches(3)
           End If
       End If
End If
Next c
End Sub
=============================================
--ron
Ron Rosenfeld - 30 Dec 2007 23:18 GMT
>only the last if  there is 1 it's should go in column 2 all others in column
>1 bassically only the last in column 2, column 2 shall not be empty
[quoted text clipped - 11 lines]
>> message>?
>> --ron

And here is another version, that uses just native VBA and avoids "Regular
Expressions"

===================================================
Option Explicit
Sub SplitSpecial()
'splits multi-word string into adjacent
'   columns as follows
'1 word --> col2
'2 words --> col1 & col2
'3 words --> 1st 2 in col1; last in col2
'4+ words --> 1st in col1; last in col3; rest in col2
Dim c As Range
Dim aStr As Variant
Dim sTemp As String
Dim i As Long

For Each c In Selection
c.Offset(0, 1).Resize(1, 3).ClearContents
   aStr = Split(Application.WorksheetFunction.Trim(c.Value), " ")
       Select Case UBound(aStr)
           Case Is = 0
               c.Offset(0, 2).Value = aStr(0)
           Case Is = 1
               c.Offset(0, 1).Value = aStr(0)
               c.Offset(0, 2).Value = aStr(1)
           Case Is = 2
               c.Offset(0, 1).Value = aStr(0) & " " & aStr(1)
               c.Offset(0, 2).Value = aStr(2)
           Case Is >= 3
               c.Offset(0, 1).Value = aStr(0)
               c.Offset(0, 3).Value = aStr(UBound(aStr))
               For i = LBound(aStr) + 1 To UBound(aStr) - 1
                   sTemp = sTemp & aStr(i) & " "
               Next i
                   c.Offset(0, 2).Value = Trim(sTemp)
       End Select
Next c
End Sub
=====================================
--ron
Rick Rothstein (MVP - VB) - 31 Dec 2007 03:47 GMT
> And here is another version, that uses just native VBA and avoids "Regular
> Expressions"

Anticipating a reply by me eh? <g>

>    aStr = Split(Application.WorksheetFunction.Trim(c.Value), " ")
>   ........
[quoted text clipped - 5 lines]
>                Next i
>                    c.Offset(0, 2).Value = Trim(sTemp)

1. Just as a point of information, the LBound an array created by the Split
function is always zero.

2. You can simplify, at least I think it would be considered a
simplification, the above code like this...

Case Is >=3
   c.Offset(0, 1).Value = aStr(0)
   c.Offset(0, 3).Value = aStr(UBound(aStr))
   aStr(0) = ""
   aStr(UBound(aStr)) = ""
   c.Offset(0, 2).Value = Trim(Join(aStr, " "))

Again, as a point of information, the default delimiter for the Split and
Join functions is a space character, so the 2nd argument in both of those
function calls could be omitted.

Rick
Rick Rothstein (MVP - VB) - 31 Dec 2007 05:19 GMT
> 1. Just as a point of information, the LBound an array created by the
> Split function is always zero.

The above was supposed to have said, the LBound for an array created by the
Split function is always zero **no matter what the Option Base setting is**.

Rick
Ron Rosenfeld - 31 Dec 2007 10:32 GMT
On Sun, 30 Dec 2007 22:47:17 -0500, "Rick Rothstein \(MVP - VB\)"
<rickNOSPAMnews@NOSPAMcomcast.net> wrote:

>Again, as a point of information, the default delimiter for the Split and
>Join functions is a space character, so the 2nd argument in both of those
>function calls could be omitted.

Yes, that's true.  But that gets us back into the discussion we've had before
about omitting defaults, and how, under certain circumstances, it can be
confusing.

--ron
Ron Rosenfeld - 31 Dec 2007 11:24 GMT
>On Sun, 30 Dec 2007 22:47:17 -0500, "Rick Rothstein \(MVP - VB\)"
><rickNOSPAMnews@NOSPAMcomcast.net> wrote:
[quoted text clipped - 8 lines]
>
>--ron

I meant to add, "especially for the LBound of the Split function, as that's
where I've been burned".
--ron
dk - 09 Jan 2008 20:50 GMT
can you please explain exactly how what &when to use this macro we are a new
user please explan step by step wehave wasted hours already
Thank You

> > And here is another version, that uses just native VBA and avoids "Regular
> > Expressions"
[quoted text clipped - 29 lines]
>
> Rick
Ron Rosenfeld - 09 Jan 2008 22:22 GMT
>can you please explain exactly how what &when to use this macro we are a new
>user please explan step by step wehave wasted hours already
>Thank You

Here's what I wrote before, although I used the term 'UDF' where I should have
used 'Macro'.

------------------------------
To enter the UDF, <alt-F11> opens the VBEditor.  Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this UDF, select your range of cells.  <alt-F8> opens the Macro dialog
box.  Select the Macro, and <run>.
-----------------------------------------

I believe the above answers your "how" question.  And I really don't know how
to make it more clear.

You will have to describe what happened as you go through each of the above
steps in order for me to help further.

As far as your "when" question, I thought that would be obvious -- you use it
when you want to split data as you requested.

The "what" question is that which you asked.
--ron
dk - 10 Jan 2008 00:28 GMT
the macro or vb don't understand because the first name doesn't  work out
with the trim function so we want to use the macro

> >only the last if  there is 1 it's should go in column 2 all others in column
> >1 bassically only the last in column 2, column 2 shall not be empty
[quoted text clipped - 53 lines]
> =====================================
> --ron
Ron Rosenfeld - 10 Jan 2008 02:22 GMT
>the macro or vb don't understand because the first name doesn't  work out
>with the trim function so we want to use the macro

What happened when you followed the instructions in my post???

I have repeated it below. Go through this paragraph by paragraph (starting with
paragraph 4 where I wrote <alt-F11>) and tell me exactly what you did and what
the result was.

=============================================
If I understand you correctly (and you can check by looking at the comments at
the top of this VBA macro), then this should do what you want.

It could be done with formulas, but it would be exceedingly complex and time
consuming to devise.

The Macro could possibly be simplified, but I believe this solution will work.

To enter the Macro, <alt-F11> opens the VBEditor.  Ensure your project is
highlighted in the Project Explorer window, then Insert/Module and paste the
code below into the window that opens.

To use this Macro, select your range of cells.  <alt-F8> opens the Macro dialog
box.  Select the Macro, and <run>.

Let me know if this does what you need.

===================================================
Option Explicit
Sub SplitSpecial()
'splits multi-word string into adjacent
'   columns as follows
'1 word --> col2
'2 words --> col1 & col2
'3 words --> 1st 2 in col1; last in col2
'4+ words --> 1st in col1; last in col3; rest in col2
Dim c As Range
Dim re As Object, mc As Object, m As Object
Dim i As Long
Set re = CreateObject("vbscript.regexp")
re.Pattern = "^(\w+\b)?(\s*(.*?)\s*)(\b\w+$)"

For Each c In Selection
c.Offset(0, 1).Resize(1, 3).ClearContents
If re.test(c.Text) Then
   Set mc = re.Execute(c.Text)
       If mc(0).submatches.Count > 0 Then
           If InStr(1, mc(0).submatches(2), " ") = 0 Then
               c.Offset(0, 1).Value = Trim(mc(0).submatches(0) & _
                   " " & mc(0).submatches(2))
               c.Offset(0, 2).Value = mc(0).submatches(3)
           Else
               c.Offset(0, 1).Value = mc(0).submatches(0)
               c.Offset(0, 2).Value = mc(0).submatches(2)
               c.Offset(0, 3).Value = mc(0).submatches(3)
           End If
       End If
End If
Next c
End Sub
=============================================
--ron
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.