PDA

View Full Version : Arrange columns of names & addresses into rows



JohnnyBravo
04-27-2010, 10:10 AM
I've got a list of names and addresses that I'm trying to organize for a mail merge. These list of names are in a CSV format, I've attached a file as an example. What I am trying to achieve is the final product which looks like the 2nd screenshot. As you can see from the screenshot, there is no consistency where the addresses are. Some are in one cell, some are on 2 cells... to make things worse, the list of names & addresses are in a 3 column format.

What is the best way to achieve this? Is VBA script the most appropriate solution or is there an Excel function that would do the job?

Note: I had to upload the excel file as an *.xlsx file - as the forum does not allow for *.csv file attachments.

http://s4.postimage.org/Ojk5r.jpg (http://www.postimage.org/image.php?v=aVOjk5r)

http://s3.postimage.org/5E2n0.jpg (http://www.postimage.org/image.php?v=Pq5E2n0)

mdmackillop
04-27-2010, 11:11 AM
Here's a start. It depends upon the length of the zipcode in each address

Option Explicit
Sub address()
Dim Arr(), a
Dim i%, j%, Col%, Rw%, x%
Dim cel As Range
ReDim Arr(5000)
For Col = 1 To 3
For Each cel In Intersect(Columns(Col), ActiveSheet.UsedRange)
If cel <> "" Then
Arr(j) = cel.Value
j = j + 1
End If
Next

Next
ReDim Preserve Arr(j - 1)
Rw = 1
i = 6
For x = 0 To UBound(Arr)
Cells(Rw, i) = Arr(x)
Cells(Rw, i).Select
If Arr(x) Like "*#####" Then
Rw = Rw + 1
i = 6
Else
i = i + 1
End If
Next

End Sub

JohnnyBravo
04-27-2010, 11:24 AM
The zip codes are always going to be 5 characters long. mdmackillop, thank you for your code - I will try it out right now and report back.

EDIT: Ok, I just tried your code and there's a few things I should clarify.

#1. I need the out of the new format in a new *.xlsx file. Name does not matter.
#2. Take a look at the 1st person John R. Doe in my screenshot above. His street address is in just one cell only compared to Jane Nobody's address. She's got 2 cells because the apartment number is located in a separate cell. When I run your code, the results is represented in the screenshot attached.
#3. Ideally, I would like to have First name in column A. Last name in column B. Zip code in column E.

http://s3.postimage.org/60wvJ.jpg (http://www.postimage.org/image.php?v=Pq60wvJ)

mdmackillop
04-27-2010, 11:45 AM
Sounds easy, but what is the last name of Thomas Jones Jr. Try splitting off the zip code yourself. Not too hard!

JohnnyBravo
04-27-2010, 11:54 AM
Sounds easy, but what is the last name of Thomas Jones Jr.

First name = Thomas
Last Name = Jones


Not too hard!

Yep - that one I can manage no problem.

mdmackillop
04-27-2010, 03:44 PM
http://www.vbaexpress.com/forum/images/quotes/quot-top-left.gifQuote:http://www.vbaexpress.com/forum/images/quotes/quot-top-right.gif http://www.vbaexpress.com/forum/images/quotes/quot-by-left.gifOriginally Posted by: mdmackillop http://www.vbaexpress.com/forum/images/quotes/quot-by-right.gifhttp://www.vbaexpress.com/forum/images/quotes/quot-top-right-10.gifSounds easy, but what is the last name of Thomas Jones Jr.http://www.vbaexpress.com/forum/images/quotes/quot-bot-left.gifhttp://www.vbaexpress.com/forum/images/quotes/quot-bot-right.gif

First name = Thomas
Last Name = Jones

I worked that out, but how does one state the logic?

JohnnyBravo
04-28-2010, 09:18 AM
I worked that out, but how does one state the logic?

Not sure what you're asking for but at any rate, to make this easier - let's just assume that all the names in my file have no jr. or sr... or any kind of suffix to deal with. In fact ,I just looked over my original csv file and out of the hundreds of names on the list, there's only 1 person with a suffix.

mdmackillop
04-28-2010, 12:44 PM
Option Explicit
Sub address()
Dim Arr(), a
Dim i%, j%, Col%, Rw%, x%
Dim cel As Range
Dim Nme As String, n As Long
ReDim Arr(5000)
For Col = 1 To 3
For Each cel In Intersect(Columns(Col), ActiveSheet.UsedRange)
If cel <> "" Then
Arr(j) = cel.Value
j = j + 1
End If
Next

Next
ReDim Preserve Arr(j - 1)
Rw = 1
i = 6
For x = 0 To UBound(Arr)
Cells(Rw, i) = Arr(x)
Cells(Rw, i).Select
If Arr(x) Like "*#####" Then
Rw = Rw + 1
i = 6
Else
If i = 6 Then
Nme = Cells(Rw, 6)
n = InStrRev(Nme, " ") - 1
Cells(Rw, 6) = Left(Nme, n)
Cells(Rw, 7) = Right(Nme, Len(Nme) - n - 1)

i = i + 2
Else: i = i + 1
End If
End If
Next

End Sub

JohnnyBravo
04-28-2010, 05:18 PM
Still not there. The City and State are not populating consistently in column J for everyone like it should. Some are in column I and some are in column J. Run your code on the sample file I attached and you'll see what I mean. Basically what I need is the screenshot that I attached in my original post.

In a new Excel spreadsheet:
Col A = should have all the First Names
Col B = should have all the Last Name
Col C = Address and Apartment number
Col D = City
Col E = Zip Code

mdmackillop
04-29-2010, 04:20 AM
Please post your own attempts at fixing the problem. We are here to assist, not to provide free solutions. Please refer to our FAQ (http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_posthelp_faq_item)

JohnnyBravo
04-29-2010, 07:25 AM
Please post your own attempts at fixing the problem. We are here to assist, not to provide free solutions. Please refer to our FAQ (http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_posthelp_faq_item)

I would if I could. I am a novice at VBA so I don't know how to modify your code. At any rate, I appreciate all the help you've provided already so thanks mate. I'll find the solution elsewhere.