Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 2000 - one column into 5 columns 5

Status
Not open for further replies.

ddamm

IS-IT--Management
Dec 11, 2002
24
0
0
US
We have a mail list of about 2500 addresses that is in a single column with blank lines as shown below. We need to convert this list into at least 5 separate columns for name, address, city, state, and zip so that our mailing software can work with the list. The city, state and zip information occupies a single cell. Any ideas on the best way to do this?

blank line
name 1
blank line
address 1
city state zip
blank line
blank line
name 2
blank line
address 2
city state zip
blank line
 
Data > Text to Columns > follow the wizard, spec out where your breaks fall...
 
Hi,

This should work
Code:
Sub ToColumns()
    Dim iCol As Byte, lRow As Long
    iCol = 2
    lRow = 2
    For Each c In Selection
        With c
            If .Value <> &quot;&quot; Then
                Select Case iCol
                    Case 2, 3
                        Cells(lRow, iCol).Value = c.Value
                    Case 4
                        sValue = &quot;&quot;
                        j = 2
                        For i = Len(c.Value) To 1 Step -1
                            sByte = Mid(c.Value, i, 1)
                            If sByte = &quot; &quot; Then
                                Cells(lRow, iCol + j).Value = sValue
                                sValue = &quot;&quot;
                                j = j - 1
                            Else
                                sValue = sByte & sValue
                            End If
                            If j < 0 Then Exit For
                        Next
                        Cells(lRow, iCol).Value = sValue
                End Select
                iCol = iCol + 1
            End If
        End With
        If iCol > 4 Then
            iCol = 2
            lRow = lRow + 1
        End If
    Next
End Sub
:)

Skip,
Skip@TheOfficeExperts.com
 
ddamm,

You know, parsing city state zip cannot work predicatable WITHOUT a unique delimiter because you have such cases as...
Code:
Elmyra New York 99999
North Huntington Rhode Island 99999
West El Paso Texas 99999
I jumped to a conculsion with my code and it will not work in all cases!!!!!



[blush]

Skip,
Skip@TheOfficeExperts.com
 
That didn't quite work. Using &quot;space&quot; as a delimiter, the name was on one row, address on the second, and city state zip on the third. The list did break into 3 columns. So, that's a step in the right direction. Using &quot;tab&quot; as a delimiter didn't work - the list stayed in a single column. I am being told that our software will parse the city state and zip if we cannot separate it. So we could work with 3 columns (name, address, city-state-zip) instead of 5.
 
You know that if you choose &quot;fixed width&quot; you can point and click, inserting breaks as you dictate, in the next step of the wizard, right? So, if you have a standard gap between elements, this should work...'course I'm making a lot of guesses about your data structure....
 
Did you copy and paste my code.

Each record is placed in a single row.

To get everyting into 3 columns just change
Code:
 Case 2, 3
to
Code:
 Case 2, 3, 4
and comment out Case 4

:)

Skip,
Skip@TheOfficeExperts.com
 
If you need to get rid of the blank rows, fill a column with ascending numbers. Sort your data by col. a (where your name, address, etc. is located), then highlight the actual data and the numbers and resort by the numbers - then you'll just have your data to tweak.
 
Just because I like making formulas:

in B1, put =IF(ROW(A1)-(INT(ROW(A1)/6)*6)=1,A2,&quot;&quot;)

and then copy and paste in down.

in C1, put =IF(ROW(A1)-(INT(ROW(A1)/6)*6)=1,A4,&quot;&quot;)

and copy and paste down.

in d1, put =IF(ROW(A1)-(INT(ROW(A1)/6)*6)=1,A5,&quot;&quot;)

and then copy and paste down.

Blue
 
I blush :)

Hey ddamm, give me an example of one of your city/st/zip's

Blue
 
Carrr,
All the info is in column A, so cannot snap a line without going through other information. Would have been a simple solution.

Skip,
Tried the original macro. Lines 1-80 parsed correctly. Then the next record starts in column 3 and continues like that for a while. Then the first name starts to appear in various places such that the records 'wrap' from one line to the next. It ran like that until line 1920. All of the records after that are untouched.
I Remed out case 4, but it doesn't seem to be working correctly. Must have missed something.

Blue Dragon,
Slick formula! Works! mscallisto is right. Very cute.
An example of a record:
Blank line
Susan Doe
Blank line
5605 Elizabeth Loop NE
Auburn, WA 98092
Blank line

Thanks,
David

 
Does the pattern change at line 81?

You might have to modify the code if there are SPACE characters in those blank rows...
Code:
If Trim(.Value) <> &quot;&quot; Then
:)

Skip,
Skip@TheOfficeExperts.com
 
With you data sampl, I will assume that the zip is always 5 digits and the city and state are divided by a ,:

City:

=LEFT(D1,FIND(&quot;,&quot;,D1)-1)

State:

=MID(D1,FIND(&quot;,&quot;,D1)+2,LEN(D1)-FIND(&quot;,&quot;,D1)-6)

Zip:

=RIGHT(D1,5)

This can be modified for 9 digit zips, or a combonation of both...

Blue
 
You are right Skip, that is why I said, if you have 9 digit zips or such, it has to be modified to allow for them...

Blue
 
Skip,
The change at line 81 is due to the fact that the record is missing the street address (it only has the name, city-state-zip).

Blue,
Some of the zip codes are 9 digits (99999-9999), but they are few and far between. With Skip's code, they break into two columns. The city is always separated by a comma from the state.

This list doesn't have any Canadian addresses, but we do a number of mailings that do. I troubleshoot the mailing software once in a while, but I am not the guy that runs it.

To modify the VB code for 9 digits, would I change the line to =RIGHT(D1,9)?

Thanks for the suggestions,

David
 
Hey guys,

Don't sweat the Candians. I think we can have the mailing software kick them out and deal with them separately.

David
 
ddamm, You would have:

=IF(MID(D1,LEN(D1)-4,1)=&quot;-&quot;,MID(D1,FIND(&quot;,&quot;,D1)+2,LEN(D1)-FIND(&quot;,&quot;,D1)-11),MID(D1,FIND(&quot;,&quot;,D1)+2,LEN(D1)-FIND(&quot;,&quot;,D1)-6))

for the state, and

=IF(MID(D1,LEN(D1)-4,1)=&quot;-&quot;,RIGHT(D1,10),RIGHT(D1,5))

for the zip

Blue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top