+ Reply to Thread
Results 1 to 8 of 8

VBA to reorganize long column into multiple rows?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-20-2017
    Location
    Boston, USA
    MS-Off Ver
    Excel 2016
    Posts
    3

    VBA to reorganize long column into multiple rows?

    Hi all,

    I'm semi familiar with macros but seem to be stumped by this issue and couldn't find anything online to help. I have a bunch of long (over 1000 rows each) excel columns that I need to reorganize into rows with common headings.

    Currently, the data is organized as follows, in one column:

    Plant name
    Jay Enterprise
    Plant Manager
    John Jay
    Date of closing
    Aug-16
    Plant size
    250,000 Sq Ft
    Employees
    7500
    Plant name
    Jay Enterprise
    Plant Manager
    John Jay
    Date of closing
    Aug-16
    Plant size
    250,000 Sq Ft
    Employees
    7500
    Plant name
    Jay Enterprise
    Plant Manager
    John Jay
    Date of closing
    Aug-16
    Plant size
    250,000 Sq Ft
    Employees
    7500

    Is there a macro I can use to split up the single column into multiple columns all beginning with the heading 'Plant Name'? As a side note, not all of the data is the same number of cells. Some of the columns would have to be 21 cells, others 18, etc etc., so I cannot use the offset function.

    Any help would be greatly appreciated!

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,755

    Re: VBA to reorganize long column into multiple rows?

    In your example, all have the same name "Plant Name" and then there are 9 more rows for that entity. In real life, how will we distinguish the Plant Name and how many rows to move? Can you show us a realistic example that we can work with. Is the term "Plant Name" the determinant to separate the records?
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    04-20-2017
    Location
    Boston, USA
    MS-Off Ver
    Excel 2016
    Posts
    3

    Re: VBA to reorganize long column into multiple rows?

    In real life the plant names will all be different. I'll provide an example of the actual data below:

    Plant Name:
    Central Investment Corp
    SIC:
    2086
    Address:
    1820 Nussbaum Parkway
    Manufactured:
    Soft drink bottling
    City, state, zip:
    Mansfield, Oh 44906
    # people:
    40
    Facility size, sq ft:
    100,000
    Established:
    1949
    Plant Manager:
    Mike O
    Closing:
    5-Oct
    Production Manager:
    Bill S
    Plant Name:
    Bob’s Candies
    SIC:
    2064
    Address:
    1315 W Oakridge Drive
    Manufactured:
    Hard candy
    City, state, zip:
    Albany, Ga 31707


    Ideally, I would have a column for each plant, so plant name is the delimiter.

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: VBA to reorganize long column into multiple rows?

    Try this for results on sheet2.
    Sub MG20Apr31
    Dim Rng As Range, Dn As Range, c As Long, Ac As Long
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    ReDim Ray(1 To Rng.Count, 1 To 1)
    For Each Dn In Rng
        If Dn.Value Like "Plant name*" Then
            c = c + 1: Ac = 0
        End If
        Ac = Ac + 1
        If Ac > UBound(Ray, 2) Then ReDim Preserve Ray(1 To Rng.Count, 1 To Ac)
        Ray(c, Ac) = Dn.Value
    Next Dn
    With Sheets("Sheet2").Range("A1").Resize(c, UBound(Ray, 2))
        .Value = Ray
        .Borders.Weight = 2
         .Columns.AutoFit
     End With
    End Sub
    Regards Mick
    Last edited by MickG; 04-20-2017 at 11:40 AM.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to reorganize long column into multiple rows?

    Maybe:

    Sub bpenick()
    Dim i As Long, y, x As Long, textrange As Range
    ReDim y(1 To Range("A" & Rows.Count).End(3).row)
    For i = UBound(y) To LBound(y) Step -1
        If Cells(i, "A") = "Plant name" Then Cells(i, "A").Insert xlDown
    Next i
    For Each textrange In Columns(1).SpecialCells(2).Areas
    x = Cells(2, Columns.Count).End(xlToLeft).Column + 1
        addr = textrange.Address(False, False)
        Range(addr).Cut Cells(2, x)
    Next textrange
    Rows(1).Delete
    Columns(1).Delete
    End Sub

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,755

    Re: VBA to reorganize long column into multiple rows?

    In John Davis' code (really nice code) BTW, change this line

    If Cells(i, "A") = "Plant name" Then Cells(i, "A").Insert xlDown
    to

    If Cells(i, "A") = "Plant Name:" Then Cells(i, "A").Insert xlDown
    to agree with your example in Post 3

    In MickG's code, you will need to capitalize Name similarly.

  7. #7
    Registered User
    Join Date
    04-20-2017
    Location
    Boston, USA
    MS-Off Ver
    Excel 2016
    Posts
    3

    Re: VBA to reorganize long column into multiple rows?

    Thank you all! The formulas work pretty well. I was able to pretty much reformat the data as I wanted.

    Best,

    Ben

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: VBA to reorganize long column into multiple rows?

    You're welcome. Glad to help out and thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Take multiple column'S values and APPEND 1 long consecutive column
    By jcpreta in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-28-2015, 07:34 AM
  2. I need to sort data from one long column to multiple rows
    By Iterationfail in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-20-2013, 07:18 AM
  3. Transpose a few rows of a very long column
    By mchapman47 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-27-2012, 01:43 PM
  4. Need Macro to Compare Columns and then Reorganize Rows
    By WildSpreadsheets in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-12-2011, 11:03 PM
  5. Rows and columns to one long column
    By kasenator in forum Excel General
    Replies: 3
    Last Post: 06-20-2007, 03:03 PM
  6. Replies: 0
    Last Post: 07-31-2006, 12:13 PM
  7. Need to split one long column into multiple rows
    By agbiggs@hotmail.com in forum Excel General
    Replies: 2
    Last Post: 03-07-2006, 04:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1