+ Reply to Thread
Results 1 to 3 of 3

Reshape data and unpivot

Hybrid View

  1. #1
    Registered User
    Join Date
    06-29-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    97

    Reshape data and unpivot

    Hello all,

    I am new to macros and beginning to gain an understanding...not quite at the point where I can write my own yet.

    I am looking to take a worksheet and unpivot the data and delete a few columns in the process. I have attached a workbook as an example. The tab "original" has the data in it's current form. The tab "unpivoted" has the columns I want and in the format i am trying to put them in.

    Thank you for your help in advance!

    -HP
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-16-2015
    Location
    Antwerpen, Belgium
    MS-Off Ver
    2007-2016
    Posts
    2,380

    Re: Reshape data and unpivot

    This will do If

    First Year start in Column G
    Every Year has 7 columns
    Between every Year is a blank column


    Sub test()
    Dim lr As Long, qtyears As Long, col As Long, x As Long
    With Sheets("Original")
        lr = .Range("A" & Rows.Count).End(xlUp).Row
        qtyears = .Cells(1, .Columns.Count).End(xlToLeft).Column - 5
        Sheets("Unpivoted").Range("A1").Resize(1, 8) = Array(.Range("A1"), .Range("B1"), .Range("C1"), .Range("D1"), .Range("E1"), .Range("F1"), "Year", "Total")
        Sheets("Unpivoted").Range("G1", "H1").Font.Bold = True
        For x = 1 To qtyears / 8
            .Range("A2", "F" & lr).Copy Sheets("Unpivoted").Range("A" & Rows.Count).End(xlUp).Offset(1)
        Next
        For col = 7 To qtyears Step 8
            myyear = Left(.Cells(1, col), 4)
            Sheets("Unpivoted").Range("G" & Rows.Count).End(xlUp).Offset(1).Resize(lr - 1, 1) = myyear
            .Cells(2, col + 6).Resize(lr - 1, 1).Copy Sheets("Unpivoted").Range("H" & Rows.Count).End(xlUp).Offset(1)
        Next
    End With
    Sheets("Unpivoted").Columns.AutoFit
    End Sub
    Kind regards
    Leo
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    06-29-2012
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    97

    Re: Reshape data and unpivot

    This is perfect. Thank you.

    I know this may be a stretch but would it be too much to ask to pseudo-code that script so in case the structure changes, I would be able to recognize which line in the code drives that?

+ 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. Unpivot Data
    By naveeddil in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 07-07-2015, 08:04 AM
  2. Unpivot and stack data
    By yching in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-14-2015, 05:18 AM
  3. Unpivot Data
    By Olly in forum Tips and Tutorials
    Replies: 0
    Last Post: 04-02-2014, 05:33 PM
  4. group data by row then reshape
    By rloiselle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2012, 06:55 PM
  5. Need 'unpivot' help for attached spreadsheet data.
    By ReneeM787 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-09-2012, 12:21 PM
  6. Lookup function to reshape data
    By prawer in forum Excel General
    Replies: 2
    Last Post: 06-17-2009, 02:25 PM
  7. [SOLVED] How to reshape data sets between long and short form as in stata?
    By Phillip Killicoat in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2005, 11:05 AM

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