+ Reply to Thread
Results 1 to 3 of 3

Unpivot data macro

Hybrid View

logisticsexcel Unpivot data macro 06-01-2017, 02:36 AM
AlphaFrog Re: Unpivot data macro 06-01-2017, 03:54 AM
logisticsexcel Re: Unpivot data macro 06-02-2017, 01:17 AM
  1. #1
    Registered User
    Join Date
    12-22-2014
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    89

    Unpivot data macro

    Hi All,

    I have some data that I want to have it in such a format that I can run a pivot table on.

    In attached file data shows original format and result shows the format I want it in.
    These can be at times 7000 rows so the result could end up in 80000 rows

    Can a VBA code do this?

    Thanks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Unpivot data macro

    Try this...

    Sub Transposer()
        
        Dim v As Variant, Headers As Variant, c As Long, r As Long, i As Long
        
        With Sheets("Data").Range("A1").CurrentRegion
            Headers = .Rows(1).Value
            v = .Offset(1).Resize(.Rows.Count - 1).Value
        End With
        
        Application.ScreenUpdating = False
        
        Sheets.Add After:=Sheets(Sheets.Count)
        Range("A1:D1").Value = Array("Material", "Plant", "Qty", "Month-Year")
        Columns("D").NumberFormat = "mmm-yy"
        
        i = UBound(v, 1)
        r = 2   'start row
        
        For c = 3 To UBound(v, 2)
            Range("A" & r).Resize(i).Value = Application.Index(v, 0, 1)
            Range("B" & r).Resize(i).Value = Application.Index(v, 0, 2)
            Range("C" & r).Resize(i).Value = Application.Index(v, 0, c)
            Range("D" & r).Resize(i).Value = Headers(1, c)
            r = r + i
        Next c
        
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    12-22-2014
    Location
    Australia
    MS-Off Ver
    2016
    Posts
    89

    Re: Unpivot data macro

    Thanks Alpha frog. It works as expected

+ 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. [SOLVED] Formula to unpivot data
    By chullan88 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-10-2017, 03:55 AM
  2. Reshape data and unpivot
    By hpatel517 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2016, 12:22 PM
  3. Unpivot Macro
    By nigelbloomy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-02-2015, 05:25 PM
  4. Unpivot Data
    By naveeddil in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 07-07-2015, 08:04 AM
  5. Unpivot and stack data
    By yching in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-14-2015, 05:18 AM
  6. Unpivot Data
    By Olly in forum Tips and Tutorials
    Replies: 0
    Last Post: 04-02-2014, 05:33 PM
  7. 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

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