Transpose Data

vanwooten

Board Regular
Joined
Dec 15, 2020
Messages
51
Office Version
  1. 365
Platform
  1. Windows
I have Excel data for area codes associated with zip codes that looks like this:

53050​
215​
267​
445​
484​
610​
835​
856​

I need the data to look like this:

53050​
215​
53050​
267​
53050​
445​
53050​
484​
53050​
610​
53050​
835​
53050​
856​

The zip codes have a varying number area codes associated. I have Access if a query would do it better. I also have some VBA experience. Any ideas?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Sorry, I underestimated the number of results. The TRANSPOSE function has a limit. This version will allow up to 100000 output rows, which can be easily raised if necessary.

VBA Code:
Sub ReformatCodes()
Dim InCell As Range, OutCell As Range, r As Long, c As Long
Dim op(1 To 100000, 1 To 2), r2 As Long

    Set InCell = Sheets("Sheet19").Range("E3")
    Set OutCell = Sheets("Sheet19").Range("A3")
  
    OutCell.Resize(1, 2).Value = InCell.Resize(1, 2).Value
  
    r = 1
    r2 = 1
    While InCell.Offset(r) <> ""
        c = 1
        While InCell.Offset(r, c) <> ""
            op(r2, 1) = InCell.Offset(r)
            op(r2, 2) = InCell.Offset(r, c)
            r2 = r2 + 1
            If r2 > 100000 Then
                MsgBox "Number of output rows is over 100000."
                Exit Sub
            End If
            c = c + 1
        Wend
        r = r + 1
    Wend
      
    OutCell.Offset(1).Resize(100000, 2) = op
End Sub
Here is another question. I now have a two column list of zip/area code combinations. What I need is a unique list of area codes with an associated Zip. This zip can be any single zip in this new two column list that is associated with the area code. Trying to produce a geographic heat map from our members' area codes since we don't have their zips.
Perfect. Thank You.
If we could reverse the original process and use this new data set to create a data set that has distinct area code as the first column followed by a horizontal list of zips I could simply take the first column of zips.
 
Upvote 0
See if this works for you:

VBA Code:
Sub ZIPsInAreaCode()
Dim InputCell As Range, OutputCell As Range, tbl As Variant
Dim lr As Long, last As Long, i As Long, r As Long, c As Long

    Set InputCell = Sheets("Sheet20").Range("A3")
    Set OutputCell = Sheets("Sheet20").Range("D3")
    
    lr = InputCell.End(xlDown).Row - InputCell.Row + 1
    tbl = WorksheetFunction.Sort(InputCell.Offset(1).Resize(lr - 1, 2), Array(2, 1), Array(1, 1))
    
    Application.ScreenUpdating = False
    OutputCell.Offset(, 1) = InputCell
    OutputCell = InputCell.Offset(, 1)
    
    last = -1
    c = 1
    r = 0
    For i = 1 To UBound(tbl)
        If tbl(i, 2) <> last Then
            r = r + 1
            c = 1
            last = tbl(i, 2)
            OutputCell.Offset(r) = tbl(i, 2)
        End If
        OutputCell.Offset(r, c) = tbl(i, 1)
        c = c + 1
    Next i
    
    Application.ScreenUpdating = True
    
End Sub

It turns this A3 table to the D3 table:

Book1
ABCDEFGH
1
2
3ZIP CodeArea codesArea codesZIP Code
453050123123530505307097123
55305026722297123
65305045626253060
75305048426753050
85305061033397123
953050835456530505307099901
105305085648453050
115306026261053050
12530704566785307097123
135307012371799901
145307067883553050
159712322285653050
169712333391999901
179712312391999901
1897123678
1999901717
2099901456
2199901919
22
Sheet20
 
Upvote 0
Solution
Glad we could help. :)

See if this works for you:

VBA Code:
Sub ZIPsInAreaCode()
Dim InputCell As Range, OutputCell As Range, tbl As Variant
Dim lr As Long, last As Long, i As Long, r As Long, c As Long

    Set InputCell = Sheets("Sheet20").Range("A3")
    Set OutputCell = Sheets("Sheet20").Range("D3")
   
    lr = InputCell.End(xlDown).Row - InputCell.Row + 1
    tbl = WorksheetFunction.Sort(InputCell.Offset(1).Resize(lr - 1, 2), Array(2, 1), Array(1, 1))
   
    Application.ScreenUpdating = False
    OutputCell.Offset(, 1) = InputCell
    OutputCell = InputCell.Offset(, 1)
   
    last = -1
    c = 1
    r = 0
    For i = 1 To UBound(tbl)
        If tbl(i, 2) <> last Then
            r = r + 1
            c = 1
            last = tbl(i, 2)
            OutputCell.Offset(r) = tbl(i, 2)
        End If
        OutputCell.Offset(r, c) = tbl(i, 1)
        c = c + 1
    Next i
   
    Application.ScreenUpdating = True
   
End Sub

It turns this A3 table to the D3 table:

Book1
ABCDEFGH
1
2
3ZIP CodeArea codesArea codesZIP Code
453050123123530505307097123
55305026722297123
65305045626253060
75305048426753050
85305061033397123
953050835456530505307099901
105305085648453050
115306026261053050
12530704566785307097123
135307012371799901
145307067883553050
159712322285653050
169712333391999901
179712312391999901
1897123678
1999901717
2099901456
2199901919
22
Sheet20
Thanks again.
 
Upvote 0

Forum statistics

Threads
1,221,869
Messages
6,162,530
Members
451,773
Latest member
ssmith04

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