Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
MG
 
Posts: n/a
Default Transpose rows to columns w/varying numbers of lines per record

I have a text-file address listing with a couple of thousand entries of
varying number of lines--some have 3 lines:

Name
Address
City, State Zip

and others have 4:

Name
Etc (like "c/o" or contact name--no particular pattern to this)
Address
City, State Zip

There are no blank lines between records. I don't have any control over the
format of the text file, as it's an extract from another program and I can't
change the way the data is extracted.

I can't use Excel's transpose feature to get a useful spreadsheet because
the records aren't consistent in the number of lines per record. Is there a
way to convert this data to a spreadsheet with 4 columns per record (with a
blank column for the 3-line records)?
  #2   Report Post  
Duke Carey
 
Posts: n/a
Default Transpose rows to columns w/varying numbers of lines per record

You could use a VBA routine, but only if there is some discernible way to
tell when one address ends and the next begins. So...is there a ZIP at the
very end of EVERY SINGLE ADDRESS? Is it always a ZIP or is it sometimes a
ZIP+4? Any non-US addresses using a nonnumeric postal code?

Tell people here how to objectively determine when to end one address and
start another, and you'll probably get some valuable help

"MG" wrote:

I have a text-file address listing with a couple of thousand entries of
varying number of lines--some have 3 lines:

Name
Address
City, State Zip

and others have 4:

Name
Etc (like "c/o" or contact name--no particular pattern to this)
Address
City, State Zip

There are no blank lines between records. I don't have any control over the
format of the text file, as it's an extract from another program and I can't
change the way the data is extracted.

I can't use Excel's transpose feature to get a useful spreadsheet because
the records aren't consistent in the number of lines per record. Is there a
way to convert this data to a spreadsheet with 4 columns per record (with a
blank column for the 3-line records)?

  #3   Report Post  
MG
 
Posts: n/a
Default Transpose rows to columns w/varying numbers of lines per recor

Thanks, Duke. Every address ends in either a ZIP or a ZIP+4.

"Duke Carey" wrote:

You could use a VBA routine, but only if there is some discernible way to
tell when one address ends and the next begins. So...is there a ZIP at the
very end of EVERY SINGLE ADDRESS? Is it always a ZIP or is it sometimes a
ZIP+4? Any non-US addresses using a nonnumeric postal code?

Tell people here how to objectively determine when to end one address and
start another, and you'll probably get some valuable help

"MG" wrote:

I have a text-file address listing with a couple of thousand entries of
varying number of lines--some have 3 lines:

Name
Address
City, State Zip

and others have 4:

Name
Etc (like "c/o" or contact name--no particular pattern to this)
Address
City, State Zip

There are no blank lines between records. I don't have any control over the
format of the text file, as it's an extract from another program and I can't
change the way the data is extracted.

I can't use Excel's transpose feature to get a useful spreadsheet because
the records aren't consistent in the number of lines per record. Is there a
way to convert this data to a spreadsheet with 4 columns per record (with a
blank column for the 3-line records)?

  #4   Report Post  
Duke Carey
 
Posts: n/a
Default Transpose rows to columns w/varying numbers of lines per recor

If you need help with where to put VBA code, look at this site

http://www.contextures.com/xlvba01.html
or
http://www.cpearson.com/excel/codemods.htm


Try this code:

Sub ParseAddresses()
Dim rngSrc As Range
Dim rngTgt As Range
Dim intLineNum As Integer

' change the next line to reflect the first cell of your data
Set rngSrc = Range("A1")

Set rngTgt = Range("C1")

intLineNum = 0
Do While True
If Len(rngSrc.Text) = 0 Then Exit Sub

If Not IsNumeric(Right(rngSrc.Text, 4)) Then
' part of the body of the address
rngTgt.Offset(0, intLineNum).Value = rngSrc.Text
intLineNum = intLineNum + 1
Set rngSrc = rngSrc.Offset(1, 0)
Else
rngTgt.Offset(0, 3).Value = rngSrc.Text
intLineNum = 0
Set rngSrc = rngSrc.Offset(1, 0)
Set rngTgt = rngTgt.Offset(1, 0)
' it's PRESUMABLY a zip code & the final address line
End If
Loop

End Sub


  #5   Report Post  
MG
 
Posts: n/a
Default Transpose rows to columns w/varying numbers of lines per recor

It successfully placed A1 in C1, A2 in D1, and A3 in E1 (the first record is
a 3-line record) but then placed A4 in F1, A5 in G1 and A6 in H1 (the second
record is also a 3-line record). It ran into the 256-column limit,
apparently, and returned runtime error 1004 for application-defined or
object-defined error. It's definitely on the right track, though. The
records referred to in the 256 columns do include both 3- and 4-line records,
just no new row for each record.

Would it be easier to write a piece of code that would insert a blank line
after each 3-line record, and then use transpose to get the 4 column list?

"Duke Carey" wrote:

If you need help with where to put VBA code, look at this site

http://www.contextures.com/xlvba01.html
or
http://www.cpearson.com/excel/codemods.htm


Try this code:

Sub ParseAddresses()
Dim rngSrc As Range
Dim rngTgt As Range
Dim intLineNum As Integer

' change the next line to reflect the first cell of your data
Set rngSrc = Range("A1")

Set rngTgt = Range("C1")

intLineNum = 0
Do While True
If Len(rngSrc.Text) = 0 Then Exit Sub

If Not IsNumeric(Right(rngSrc.Text, 4)) Then
' part of the body of the address
rngTgt.Offset(0, intLineNum).Value = rngSrc.Text
intLineNum = intLineNum + 1
Set rngSrc = rngSrc.Offset(1, 0)
Else
rngTgt.Offset(0, 3).Value = rngSrc.Text
intLineNum = 0
Set rngSrc = rngSrc.Offset(1, 0)
Set rngTgt = rngTgt.Offset(1, 0)
' it's PRESUMABLY a zip code & the final address line
End If
Loop

End Sub




  #6   Report Post  
Duke Carey
 
Posts: n/a
Default Transpose rows to columns w/varying numbers of lines per recor

The code checked the last for characters of each line to see if they were
numeric. If so it assumed it was a Zip code and that line marked the end of
the address.

What you describe sounds as if it never found a single ZIP, making me wonder
if the lines are padded with spaces to the right. Select one of the lines
that ends witha ZIP code, press the F2 key, and see if the cursor appears off
to the right of the last character. If so, it's padded with spaces,a nd a
slight modification to the code should fix it.

Change this line

If Not IsNumeric(Right(rngSrc.Text, 4)) Then

to

If Not IsNumeric(Right(Trim(rngSrc.Text), 4)) Then

and then re-run it to see what happens


"MG" wrote:

It successfully placed A1 in C1, A2 in D1, and A3 in E1 (the first record is
a 3-line record) but then placed A4 in F1, A5 in G1 and A6 in H1 (the second
record is also a 3-line record). It ran into the 256-column limit,
apparently, and returned runtime error 1004 for application-defined or
object-defined error. It's definitely on the right track, though. The
records referred to in the 256 columns do include both 3- and 4-line records,
just no new row for each record.

Would it be easier to write a piece of code that would insert a blank line
after each 3-line record, and then use transpose to get the 4 column list?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reverse order of rows and columns Johnny Excel Discussion (Misc queries) 3 September 11th 05 03:42 PM
autofill information from rows to columns without using transpose Rayce Excel Discussion (Misc queries) 1 September 4th 05 01:44 AM
Removing Near-Duplicate Rows, Leaving Those w/Most Data in Specific Columns foofoo Excel Discussion (Misc queries) 1 April 2nd 05 12:02 AM
How to keep rows together when sorting columns? Amit Excel Worksheet Functions 1 March 28th 05 06:01 PM
Add more lines and more columns in Excel Marc Charbonneau Excel Worksheet Functions 3 January 20th 05 03:44 AM


All times are GMT +1. The time now is 07:50 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"