separate text into two columns

G

gwbdirect

I would like to separate a text field (company name) into two columns
whenever it is longer than 25 characters but I want it to separate after a
space and not break in the middle of a name like the example below:
co name 1
company name 2
A & M Construction A & M Construct ion
A Better Chance, Inc. A Better Chanc e,
Inc.

Would like it to come out like the example below:
A & M Construction A & M
Construction
A Better Chance, Inc. A Better Chance, Inc.
=left(a2,10) doesn't work. Is there another formula that will
 
B

Bob Phillips

Try this

=IF(LEN(A2)>20, LEFT(A2,FIND(" ",A2,15)),A2)

=SUBSTITUTE(A2,B2,"")

tune the character position, the 15, to suit
 
G

gwbdirect

Thanks bob,
I understand how to make the firs formula work-=IF(LEN(A2)>20,
LEFT(A2,FIND(" ",A2,15)),A2) It gives me A & J Windows but how do I put
Company, Inc in the next column.
=SUBSTITUTE(A2,B2,"") gives me A & J Windows Company, Inc. which is what I
started with. What am I missing.
 
G

gwbdirect

i MUST NOT BE UNDERSTANDING BECAUSE IT COMES OUT LIKE THIS:
COLUMN 1 COLUMN 2
#VALUE! A & M Construction company inc dba AIM INDUSTRIES
COLUMN 1 COLUMN 2
I WANT IT TO BE A & M Construction company inc dba AIM INDUSTRIES
 
R

Rick Rothstein

You didn't follow the double asterisked instructions I gave you...

**Commit these formulas using Ctrl+Shift+Enter, not just Enter by itself.

Put the cursor in the formula bar and press Ctrl+Shift+Enter commit
("enter") the formulas. However, the breakup will not be as you indicated. I
broke the text up at the first space AT OR BEFORE character position 25
(assumed that is where you wanted it). The break point you now indicate you
want is at a position greater than 25. What is the rule for where you want
the break point (other than at a space which my formulas already to if you
"enter" them correctly)?
 
G

gwbdirect

Hi Rick,
I got the formula to work on the first part of the name but can't get the
second part to go into another column.
Just to be sure you understand. If I have a mailing list with 500 names,
company, titles, addresses city state zip and either the Company or Title is
more than 25 characters I have to separate the company name or title into two
columns on the spreadsheet so when I create the label the name ends up oj two
lines.
Example:
line 1 = A & M Construction company inc
line 2 = dba AIM INDUSTRIES

The software I use pulls in each column in excel and then puts makes a two
line company or title so it fits on the envelope. It doesn't automatically
truncate long names it just doesn't print them.
I still may be doing something wrong. I'll keep trying!!! Thanks for your
help so far!!
 
R

Rick Rothstein

names, company, titles, addresses city state zip

Do you have ALL of these in a single cell? If so, separated by what
character (a comma perhaps)? Or do you have each element in its own cell? If
so, which one is the company and title in?
 
G

gwbdirect

EACH FIELD IS A SEPARATE COLUMN IN EXCEL

Company =a1, Title = b1, address = c1, etc.

I will end up with company = 1st half in a1, second half in b1, title first
half in c1, second half in d1, address in e1, etc.
 
R

Rick Rothstein

You can't do that with formulas. You can't have the text in A1 and then
split that text into A1 and B1... either text is in a cell or a formula is
in a cell, but you can't have both there. What I (and others) gave you is a
way to split the text into two different cells. You can do what you want,
but only by using VB code. I'll be happy to take a shot at giving you a
macro that will do what you want, but you have to clarify something for us
first. You indicated that you wanted to split a line if it was more than 25
characters long, but then you gave this example...

line 1 = A & M Construction company inc
line 2 = dba AIM INDUSTRIES

where I am assuming the original text was this...

A & M Construction company inc dba AIM INDUSTRIES

The problem I am having is that your indicated line 1 is 30 characters long.
I would have thought if you needed to break a line if it were longer than 25
characters, then only 25 characters would be acceptable per line. Can you
clarify the rules you want to use when breaking a line apart?
 
G

gwbdirect

Sorry I wasn't clear. 25 characters including spaces is the most that will
fit on a postcard in the spce provided so if a Company name or Title is
longer I need to separate it into two columns so I can set up the address
block and print the whole name.
A & M Construction company
inc dba AIM INDUSTRIES
133 anywhere street
city state zip

A & M Construction company inc dba AIM INDUSTRIES is too long to fit on the
postcard and the customer wants everything to appear even if it has to be in
two lines.

so 25 maximum characters but I want it to break at a space and not in the
middle of a name.
My mistake my example should have been:
A & M Construction company column 1
inc dba AIM INDUSTRIES column 2

Hope this clears it up. Thanks in advance for all of your help!!!
 
R

Rick Rothstein

so 25 maximum characters but I want it to break at a space and not in the
middle of a name.
My mistake my example should have been:
A & M Construction company column 1
inc dba AIM INDUSTRIES column 2

Your "column 1" entry is 26 characters long, "company" should not be on that
line, right?

Your post also raises the question... What if the column 2 line is more than
25 characters long, do you break it into a 3rd column or just truncate it?
 
G

gwbdirect

YOU ARE CORRECT 26 CHARACTERS WHICH WOULD PUT COMPANY IN COLUMN 2. iF LINE
TWO GOES BEYOND 25 CHARACHTERS AND IT'S POSSIBLE TO SET UP THE CODE TO DO A
THIRD COLUMN THAT WOULD BE GREAT. iF NOT i WILL JUST LET IT TRUNCATE.
 
R

Rick Rothstein

Try this macro (set the values for your setup in the Const statements)...

Sub SplitCellsAt25OrLess()
Dim C As Range
Dim x As Long
Dim Space As Long
Dim LastRow As Long
Dim CellOffset As Long
Dim Text As String

Const StartRow As Long = 2
Const NameColumn As String = "A"
Const SheetName as String = "Sheet4"

With Worksheets(SheetName)
LastRow = .Cells(.Rows.Count, NameColumn).End(xlUp).Row
For x = StartRow To LastRow
CellOffset = 1
With .Cells(x, NameColumn)
Text = Trim(.Value) & " "
Space = InStrRev(Left(Text, 26), " ")
Do While Space > 0
.Offset(, CellOffset).Value = Trim(Left(Text, Space - 1))
Text = LTrim(Mid(Text, Space + 1))
Space = InStrRev(Left(Text, 26), " ")
CellOffset = CellOffset + 1
Loop
.Offset(, CellOffset).Value = Text
End With
Next
End With
End Sub

This macro will split the text in the column specified by the NameColumn
constant (the Const statement) into the adjacent columns (as many as is
needed). If you are not familiar with installing a macro, press Alt+F11 to
go into the VB editor, click Insert/Module on its menu bar and copy/paste
the above macro into the code window that opened up. To run the macro, press
Alt+F8 from the worksheet, select the macro name on the list
(SplitCellsAt25OrLess) and then click the Run button.
 
R

Ron Rosenfeld

I would like to separate a text field (company name) into two columns
whenever it is longer than 25 characters but I want it to separate after a
space and not break in the middle of a name like the example below:
co name 1
company name 2
A & M Construction A & M Construct ion
A Better Chance, Inc. A Better Chanc e,
Inc.

Would like it to come out like the example below:
A & M Construction A & M
Construction
A Better Chance, Inc. A Better Chance, Inc.
=left(a2,10) doesn't work. Is there another formula that will

Neither of your company names are longer than 25 characters, so I don't really
understand how you are separating them.

But here is a macro that will split at any length you wish. (Using 16 seems to
obtain the results you show above).

To enter this, <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 it, select the cells to be split. Then <alt-F8> opens the macro dialog
box. Select the macro and <RUN>.

As written, it splits into the adjacent columns. The macro can be edited to
"replace" the first column contents by changing the value for lDestOffset from
1 to 0.

================================
Option Explicit
Sub WordWrap16()
'Wraps at W characters, but will allow overflow if a word is longer than W
Dim re As Object, mc As Object, m As Object
Dim Str As String
Dim W As Long
Dim rSrc As Range, c As Range
Dim mBox As Long
Dim i As Long
'with offset as 1, split data will be below original data
'with offset = 0, split data will replace original data
Const lDestOffset As Long = 1

Set rSrc = Selection
If rSrc.Columns.Count <> 1 Then
MsgBox ("You may only select" & vbLf & " Data in One (1) Column")
Exit Sub
End If
Set re = CreateObject("vbscript.regexp")
re.Global = True
W = InputBox("Maximum characters in a Line: ", , 16)
If W < 1 Then W = 16
For Each c In rSrc
Str = c.Value
'remove all line feeds and nbsp
re.Pattern = "[\xA0\r\n]"
Str = re.Replace(Str, " ")
re.Pattern = "\s?((\S[\s\S]{1," & W - 2 & _
"}\S)|(\S[\s\S]{" & W - 1 & ",}?\S))(\s|$)"
If re.Test(Str) = True Then
Set mc = re.Execute(Str)
'see if there is enough room
i = lDestOffset + 1
Do Until i > mc.Count + lDestOffset
If Len(c(1, i)) <> 0 Then
mBox = _
MsgBox("Data in " & c(1, i).Address & _
" will be erased if you continue", vbOKCancel)
If mBox = vbCancel Then Exit Sub
End If
i = i + 1
Loop

i = lDestOffset
For Each m In mc
c.Offset(0, i).Value = m.SubMatches(0)
i = i + 1
Next m
End If
Next c
Set re = Nothing
End Sub
=================================

--ron
 
G

gwbdirect

Hi Ron,
Have been away on vacation. I copied the macro and ran it. Got a debug error:
With Worksheets(SheetName)
What do I need to do to fix the debug. Not very good at this!!!!


Ron Rosenfeld said:
I would like to separate a text field (company name) into two columns
whenever it is longer than 25 characters but I want it to separate after a
space and not break in the middle of a name like the example below:
co name 1
company name 2
A & M Construction A & M Construct ion
A Better Chance, Inc. A Better Chanc e,
Inc.

Would like it to come out like the example below:
A & M Construction A & M
Construction
A Better Chance, Inc. A Better Chance, Inc.
=left(a2,10) doesn't work. Is there another formula that will

Neither of your company names are longer than 25 characters, so I don't really
understand how you are separating them.

But here is a macro that will split at any length you wish. (Using 16 seems to
obtain the results you show above).

To enter this, <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 it, select the cells to be split. Then <alt-F8> opens the macro dialog
box. Select the macro and <RUN>.

As written, it splits into the adjacent columns. The macro can be edited to
"replace" the first column contents by changing the value for lDestOffset from
1 to 0.

================================
Option Explicit
Sub WordWrap16()
'Wraps at W characters, but will allow overflow if a word is longer than W
Dim re As Object, mc As Object, m As Object
Dim Str As String
Dim W As Long
Dim rSrc As Range, c As Range
Dim mBox As Long
Dim i As Long
'with offset as 1, split data will be below original data
'with offset = 0, split data will replace original data
Const lDestOffset As Long = 1

Set rSrc = Selection
If rSrc.Columns.Count <> 1 Then
MsgBox ("You may only select" & vbLf & " Data in One (1) Column")
Exit Sub
End If
Set re = CreateObject("vbscript.regexp")
re.Global = True
W = InputBox("Maximum characters in a Line: ", , 16)
If W < 1 Then W = 16
For Each c In rSrc
Str = c.Value
'remove all line feeds and nbsp
re.Pattern = "[\xA0\r\n]"
Str = re.Replace(Str, " ")
re.Pattern = "\s?((\S[\s\S]{1," & W - 2 & _
"}\S)|(\S[\s\S]{" & W - 1 & ",}?\S))(\s|$)"
If re.Test(Str) = True Then
Set mc = re.Execute(Str)
'see if there is enough room
i = lDestOffset + 1
Do Until i > mc.Count + lDestOffset
If Len(c(1, i)) <> 0 Then
mBox = _
MsgBox("Data in " & c(1, i).Address & _
" will be erased if you continue", vbOKCancel)
If mBox = vbCancel Then Exit Sub
End If
i = i + 1
Loop

i = lDestOffset
For Each m In mc
c.Offset(0, i).Value = m.SubMatches(0)
i = i + 1
Next m
End If
Next c
Set re = Nothing
End Sub
=================================

--ron
 
R

Ron Rosenfeld

Hi Ron,
Have been away on vacation. I copied the macro and ran it. Got a debug error:
With Worksheets(SheetName)
What do I need to do to fix the debug. Not very good at this!!!!

Well, you could try using the macro that *I* provided, which does not contain
that line :))

(Or you could ask Rick, who is probably the source of the macro you are writing
about).

--ron
 
G

gwbdirect

Fantastic. Sorry I must have tried Rick's first. This macro works perfectly.
Thanks for all your help!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top