rearrange a column of businesses into meaningful rows & columns

Gingertrees

Well-known Member
Joined
Sep 21, 2009
Messages
697
I'm trying to parse out the data in an Excel spreadsheet with over 10,000 lines of data. Unfortunately, it's not organized in a very helpful manner, with each line being a separate row, but every business taking up 4-6 rows, as below:

A's Business 1
400 Some Road
Cedar Falls, IA
Phone: (800) 111-2222
www.address1.com
info@address1.com
Aaron's Name place
110 L Street
Omaha, NE
Phone: (888) 222-3333
A1 Something
4 Flagstaff Rd
Rochester, NH
Phone: (800) 000-1234
www.a1something.com
brandon@a1somes.com
ABC Other Stuff
5000 Old Seward Hwy
Anchorage, AK
Phone: (800) 111-3455
www.abcothers.com
ABC Place, LLC
(etc)

If they all had 6 rows, that would be a pretty simple routine to write and perform... but the variable length, with some having 4 rows and some having 6... I just don't know what to do. Ideas?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How would the code know when one company ends and the next starts -- i.e., what is consistently consistent?
 
Upvote 0
Well, every one has a city, st (zip was not included for some reason). So you have the 3rd line for each business has a comma, space, and 2 capitalized characters. Maybe we can work with that knowledge?
 
Upvote 0
Not exactly. e.g.:
A's Business 1
400 Some Road
Cedar Falls, IA
Phone: (800) 111-2222
www.address1.com
info@address1.com
There are 2 .com lines here. Also, some lines end in .net, and others have no email/URL at all.

I believe the answer is some little looping routine. I added a 2nd column, identifying the line as "state" if it met the , ## rule. Using that, something like [sucky pretend VBA ahead]
Code:
For each cell in range,
=if cell+1 column = "state"
cell = i

copy i-2 rows into name destination
copy i-1 row into address destination
copy i into city/state destination
copy i+1 row into phone destination

Next i
 
Upvote 0
In your example there can be 1. no web or email (four lines) 2. either (five lines) 3. both (six). Is that the only varying factor?
 
Upvote 0
Hi Ariel,

Regarding using the [city] comma [state abbreviation] as always being the third line of a record: are the states all U.S. states? I ask as it might be useful for a pattern to look for.

Mark
 
Upvote 0
sheetspread: that is correct. 4-6 lines, variant by existence or not of email and URL.
GTO: hi Mark! long time no see :) Yes, only US states, and the pattern is good for all 1800+ records.
 
Upvote 0
Excel Workbook
ABC
2A's Business 11TRUE
3400 Some Road
4Cedar Falls, IA
5Phone: (800) 111-2222
6www.address1.com
7info@address1.com
8Aaron's Name place
9110 L Street
10Omaha, NE
11Phone: (888) 222-3333
12A1 Something
134 Flagstaff Rd
14Rochester, NH
15Phone: (800) 000-1234
16www.a1something.com
17brandon@a1somes.com
18ABC Other Stuff
195000 Old Seward Hwy
20Anchorage, AK
21Phone: (800) 111-3455
22www.abcothers.com
23ABC Place, LLC
Sheet3 (5)


Drag down:

Excel Workbook
ABC
2A's Business 11TRUE
3400 Some Road2TRUE
4Cedar Falls, IA3TRUE
5Phone: (800) 111-22224TRUE
6www.address1.com5TRUE
7info@address1.com6TRUE
8Aaron's Name place1TRUE
9110 L Street2TRUE
10Omaha, NE3TRUE
11Phone: (888) 222-33334TRUE
12A1 Something1insert 2 rows
134 Flagstaff Rd2TRUE
14Rochester, NH3TRUE
15Phone: (800) 000-12344TRUE
16www.a1something.com5TRUE
17brandon@a1somes.com6TRUE
18ABC Other Stuff1TRUE
195000 Old Seward Hwy2TRUE
20Anchorage, AK3TRUE
21Phone: (800) 111-34554TRUE
22www.abcothers.com5TRUE
23ABC Place, LLC11
Sheet3 (7)


Highlight column B, Use F5 (Goto) Special-Formulas-Text to distinguish the "insert 2 rows" cells, then insert 2 rows.

Highlight column B, Use F5 (Goto) Special-Formulas-Number to distinguish the "1" cells, then insert 1 rows.

Excel Workbook
ABC
2A's Business 11TRUE
3400 Some Road2TRUE
4Cedar Falls, IA3TRUE
5Phone: (800) 111-22224TRUE
6www.address1.com5TRUE
7info@address1.com6TRUE
8Aaron's Name place1TRUE
9110 L Street2TRUE
10Omaha, NE3TRUE
11Phone: (888) 222-33334TRUE
12
13
14A1 Something1insert 2 rows
154 Flagstaff Rd2TRUE
16Rochester, NH3TRUE
17Phone: (800) 000-12344TRUE
18www.a1something.com5TRUE
19brandon@a1somes.com6TRUE
20ABC Other Stuff1TRUE
215000 Old Seward Hwy2TRUE
22Anchorage, AK3TRUE
23Phone: (800) 111-34554TRUE
24www.abcothers.com5TRUE
25
26ABC Place, LLC11
Sheet3 (7)



Add headers to the right and an additional formula in D2

Excel Workbook
ABCDEFGHI
1NameAddressCity/StatePhoneWebEmail
2A's Business 11TRUEA's Business 1
3400 Some Road2TRUE
4Cedar Falls, IA3TRUE
5Phone: (800) 111-22224TRUE
6www.address1.com5TRUE
7info@address1.com6TRUE
8Aaron's Name place1TRUE
9110 L Street2TRUE
10Omaha, NE3TRUE
11Phone: (888) 222-33334TRUE
12
13
14A1 Something1insert 2 rows
154 Flagstaff Rd2TRUE
16Rochester, NH3TRUE
17Phone: (800) 000-12344TRUE
18www.a1something.com5TRUE
19brandon@a1somes.com6TRUE
20ABC Other Stuff1TRUE
215000 Old Seward Hwy2TRUE
22Anchorage, AK3TRUE
23Phone: (800) 111-34554TRUE
24www.abcothers.com5TRUE
25
26ABC Place, LLC11
Sheet3 (7)



Copy across and down:

Excel Workbook
ABCDEFGHI
1NameAddressCity/StatePhoneWebEmail
2A's Business 11TRUEA's Business 1400 Some RoadCedar Falls, IAPhone: (800) 111-2222www.address1.cominfo@address1.com
3400 Some Road2TRUEAaron's Name place110 L StreetOmaha, NEPhone: (888) 222-333300
4Cedar Falls, IA3TRUEA1 Something4 Flagstaff RdRochester, NHPhone: (800) 000-1234www.a1something.combrandon@a1somes.com
5Phone: (800) 111-22224TRUEABC Other Stuff5000 Old Seward HwyAnchorage, AKPhone: (800) 111-3455www.abcothers.com0
6www.address1.com5TRUEABC Place, LLC
7info@address1.com6TRUE
8Aaron's Name place1TRUE
9110 L Street2TRUE
10Omaha, NE3TRUE
11Phone: (888) 222-33334TRUE
12
13
14A1 Something1insert 2 rows
154 Flagstaff Rd2TRUE
16Rochester, NH3TRUE
17Phone: (800) 000-12344TRUE
18www.a1something.com5TRUE
19brandon@a1somes.com6TRUE
20ABC Other Stuff1TRUE
215000 Old Seward Hwy2TRUE
22Anchorage, AK3TRUE
23Phone: (800) 111-34554TRUE
24www.abcothers.com5TRUE
25
26ABC Place, LLC11
Sheet3 (7)
 
Upvote 0
Howdy All:

Here was my take. For an initial sheet like:
Excel Workbook
A
1HEADER
2A's Business 1
3400 Some Road
4Cedar Falls, IA
5Phone: (800) 111-2222
6www.address1.com
7info@address1.com
8Aaron's Name place
9110 L Street
10Omaha, NE
11Phone: (888) 222-3333
12A1 Something
134 Flagstaff Rd
14Rochester, NH
15Phone: (800) 000-1234
16www.a1something.com
17brandon@a1somes.com
18ABC Other Stuff
195000 Old Seward Hwy
20Anchorage, AK
21Phone: (800) 111-3455
22www.abcothers.com
23mark@somewhere.net
RawData


...and AFTER:
Excel Workbook
DEFGH
1NAMEADDRESSLOCATIONPHONEEMAIL
2A's Business 1400 Some RoadCedar Falls, IAPhone: (800) 111-2222www.address1.com
3info@address1.com
4Aaron's Name place110 L StreetOmaha, NEPhone: (888) 222-3333
5A1 Something4 Flagstaff RdRochester, NHPhone: (800) 000-1234www.a1something.com
6brandon@a1somes.com
7ABC Other Stuff5000 Old Seward HwyAnchorage, AKPhone: (800) 111-3455www.abcothers.com
8mark@somewhere.net
RawData


Using another sheet for state codes like:
Excel Workbook
ABCD
1State/TerritoryStandard AbbreviationPostal AbbreviationCapital City
2AlabamaAla.ALMontgomery
3AlaskaAlaskaAKJuneau
4American Samoan/aASPago Pago
5ArizonaAriz.AZPhoenix
State Abbreviations


In a Standard Module:
Rich (BB code):
Option Explicit
    
Sub ArielsParser()
Dim REX                     As Object '<--- RegExp
Dim rngData                 As Range
Dim Cell                    As Range
Dim aryOutput()             As String
Dim aryTranspose            As Variant
Dim lEndRow                 As Long
Dim lRow                    As Long
Dim n                       As Long
Dim nn                      As Long
Dim lLastEmail              As Long
Dim lFirstEmail             As Long
Dim lCurEmail               As Long
Dim strPattern              As String
Dim bolRuleOutLastRecord    As Boolean
    
    With shtPostalCodes
        For Each Cell In .Range(.Cells(2, 3), .Cells(.Rows.Count, 3).End(xlUp)).Cells
            strPattern = strPattern & Cell.Value & "|"
        Next
        strPattern = Left(strPattern, Len(strPattern) - 1)
        strPattern = "([A-z\ ]+\,\ +)(" & strPattern & ")"
    End With
    
    Set REX = CreateObject("VBScript.RegExp")
    With REX
        .Global = False
        .IgnoreCase = True
        .Pattern = strPattern
    End With
    ReDim aryOutput(1 To 5, 1 To 1)
    aryOutput(1, 1) = "NAME"
    aryOutput(2, 1) = "ADDRESS"
    aryOutput(3, 1) = "LOCATION"    '<---Look for
    aryOutput(4, 1) = "PHONE"
    aryOutput(5, 1) = "EMAIL"
    
    lEndRow = shtRawData.Cells(shtRawData.Rows.Count, 1).End(xlUp).Row
        
    With REX
        
        For n = 2 To lEndRow
            bolRuleOutLastRecord = False
            If .Test(shtRawData.Cells(n, 1).Value) Then
                ReDim Preserve aryOutput(1 To 5, 1 To UBound(aryOutput, 2) + 1)
                aryOutput(1, UBound(aryOutput, 2)) = shtRawData.Cells(n - 2, 1).Value
                aryOutput(2, UBound(aryOutput, 2)) = shtRawData.Cells(n - 1, 1).Value
                aryOutput(3, UBound(aryOutput, 2)) = shtRawData.Cells(n, 1).Value
                aryOutput(4, UBound(aryOutput, 2)) = shtRawData.Cells(n + 1, 1).Value
                
                For nn = n + 2 To lEndRow
                    If .Test(shtRawData.Cells(nn, 1).Value) Then
                        bolRuleOutLastRecord = True
                        lLastEmail = nn - 3
                        lFirstEmail = n + 2
                        
                        aryOutput(5, UBound(aryOutput, 2)) = shtRawData.Cells(lFirstEmail, 1).Value
                        
                        For lCurEmail = lFirstEmail + 1 To lLastEmail
                            ReDim Preserve aryOutput(1 To 5, 1 To UBound(aryOutput, 2) + 1)
                            aryOutput(5, UBound(aryOutput, 2)) = shtRawData.Cells(lCurEmail, 1).Value
                        Next
                        Exit For
                    End If
                Next
                
                If Not bolRuleOutLastRecord Then
                    aryOutput(5, UBound(aryOutput, 2)) = shtRawData.Cells(n + 2, 1).Value
                    For lCurEmail = n + 3 To lEndRow
                        ReDim Preserve aryOutput(1 To 5, 1 To UBound(aryOutput, 2) + 1)
                        aryOutput(5, UBound(aryOutput, 2)) = shtRawData.Cells(lCurEmail, 1).Value
                    Next
                End If
            End If
        Next
    End With
    
    For n = LBound(aryOutput, 1) To UBound(aryOutput, 1) - 1
        If aryOutput(5, n) = aryOutput(1, n + 1) Then aryOutput(5, n) = Empty
    Next
    
    '// IF in Excel2000, send aryOutput to another function to "manually" transpose.    //
    aryTranspose = Application.Transpose(aryOutput)
    
    shtRawData.Range("C1").Resize(UBound(aryTranspose, 1), UBound(aryTranspose, 2)).Value = aryTranspose
End Sub
Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top