+ Reply to Thread
Results 1 to 2 of 2

Help modifying Un-Pivot Macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Help modifying Un-Pivot Macro

    Hello,

    I got the un-pivot code here (the rewitten part of "snb’s approach") it is blazing fast.

    The problem is that the range to unpivot is imputed with msgboxes and I am always un-pivoting the same table with different data in it so I would like to hard code the ranges asked for.

    I have tryed but not getting it.

    There is only the pivot table on the active sheet

    The range of the headers to the LEFT not to be un-pivoted is col A to G

    The range of header to the RIGHT to be un-pivoted is col H to last column

    Below is that part of the code that ask for the prompts

    Thank you

    Sub UnPivot_snb()
    Dim varSource As Variant
    Dim j As Long
    Dim m As Long
    Dim n As Long
    Dim i As Long
    Dim varOutput As Variant
    Dim rngCrossTab As Range
    Dim rngLeftHeaders As Range
    Dim rngRightHeaders As Range
    
    'Identify where the ENTIRE crosstab table is
    If rngCrossTab Is Nothing Then
    On Error Resume Next
    Set rngCrossTab = Application.InputBox( _
    Title:="Please select the ENTIRE crosstab", _
    prompt:="Please select the ENTIRE crosstab that you want to turn into a flat file", _
    Type:=8, Default:=Selection.CurrentRegion.Address)
    If Err.Number <> 0 Then
    On Error GoTo errhandler
    Err.Raise 999
    Else: On Error GoTo errhandler
    End If
    rngCrossTab.Parent.Activate
    rngCrossTab.Cells(1, 1).Select 'Returns them to the top left of the source table for convenience
    End If
    
    'Identify range containing columns of interest running down the table
    If rngLeftHeaders Is Nothing Then
    On Error Resume Next
    Set rngLeftHeaders = Application.InputBox( _
    Title:="Select the column HEADERS from the LEFT of the table that WON'T be aggregated", _
    prompt:="Select the column HEADERS from the LEFT of the table that won't be aggregated", _
    Default:=Selection.Address, Type:=8)
    If Err.Number <> 0 Then
    On Error GoTo errhandler
    Err.Raise 999
    Else: On Error GoTo errhandler
    End If
    Set rngLeftHeaders = rngLeftHeaders.Resize(1, rngLeftHeaders.Columns.Count) 'just in case they selected the entire column
    rngLeftHeaders.Cells(1, rngLeftHeaders.Columns.Count + 1).Select 'Returns them to the right of the range they just selected
    End If
    
    If rngRightHeaders Is Nothing Then
    'Identify range containing data and cross-tab headers running across the table
    On Error Resume Next
    Set rngRightHeaders = Application.InputBox( _
    Title:="Select the column HEADERS from the RIGHT of the table that WILL be aggregated", _
    prompt:="Select the column HEADERS from the RIGHT of the table that WILL be aggregated", _
    Default:=Selection.Address, _
    Type:=8)
    If Err.Number <> 0 Then
    On Error GoTo errhandler
    Err.Raise 999
    Else: On Error GoTo errhandler
    End If
    Set rngRightHeaders = rngRightHeaders.Resize(1, rngRightHeaders.Columns.Count) 'just in case they selected the entire column
    rngCrossTab.Cells(1, 1).Select 'Returns them to the top left of the source table for convenience
    End If
    
    If strCrosstabName = "" Then
    'Get the field name for the columns being consolidated e.g. 'Country' or 'Project'. note that reserved SQL words like 'Date' cannot be used
    strCrosstabName = Application.InputBox( _
    Title:="What name do you want to give the data field being aggregated?", _
    prompt:="What name do you want to give the data field being aggregated? e.g. 'Date', 'Country', etc.", _
    Default:="Date", _
    Type:=2)
    If strCrosstabName = "False" Then Err.Raise 999
    End If

  2. #2
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Help modifying Un-Pivot Macro

    With a little more patients I got

    Sub UnPivot_snb()
    Dim varSource As Variant
    Dim j As Long
    Dim m As Long
    Dim n As Long
    Dim i As Long
    Dim varOutput As Variant
    Dim rngCrossTab As Range
    Dim rngLeftHeaders As Range
    Dim rngRightHeaders As Range
    
    'Identify where the ENTIRE crosstab table is
    Set rngCrossTab = Selection.CurrentRegion
    
    
    'Identify range containing columns of interest running down the table
    Set rngLeftHeaders = Range("A1:G1")
    
     Dim rng As Range
            Dim lastCol As Long
    
            With Sheets("sheet1")
                lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
     End With
    
    Set rngRightHeaders = Range(Cells(1, 8), Cells(1, lastCol))
    
    'Get the field name for the columns being consolidated e.g. 'Country' or 'Project'. note that reserved SQL words like 'Date' cannot be used
    strCrosstabName = "Question"

+ 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. Replies: 0
    Last Post: 06-19-2012, 06:22 AM
  2. Modifying Pivot table cell
    By junkscratch in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-25-2007, 03:41 AM
  3. [SOLVED] Modifying Macro
    By CLR in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 09:05 PM
  4. [SOLVED] Modifying Macro
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. Modifying Macro
    By carl in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM

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