Consulting

Results 1 to 5 of 5

Thread: Normalise Pivot Table

  1. #1

    Normalise Pivot Table

    Hi all,

    I want to normalise a pivot table using VBA.

    Currently my sheet looks as follows:

    Field1, Field2, Date1, Date2, Date3, Date4
    key1, key2, Value1, Value2, Value3, Value4

    However, I want to view it as:

    Field1, Field2, Date, Value
    key1, key2, Date1, Value1
    key1, key2, Date2, Value2

    So far I have the following VBA code, but it only normalise based on the Field1. I know I need to include another loop but I can't seem to perfect it
    Sub Normalise()
    
    
    Dim wsOriginal As Worksheet
    Dim wsNormalized As Worksheet
    Dim strKey As String
    Dim clnHeader As Collection
    Dim lngColumnCounter As Long
    Dim lngRowCounterOriginal As Long
    Dim lngRowCounterNormalized As Long
    Dim rngCurrent As Range
    Dim varColumn As Variant
    
    
        Set wsOriginal = ThisWorkbook.Worksheets("Forecast14")     
        Set wsNormalized = ThisWorkbook.Worksheets("Sheet1") 
        Set clnHeader = New Collection
        
        wsNormalized.Cells.ClearContents        
    
    
        lngColumnCounter = 1
        lngRowCounterOriginal = 1
        Set rngCurrent = wsOriginal.Cells(lngRowCounterOriginal, lngColumnCounter)
        
        ' Loop through headers to get header names
        Do Until IsEmpty(rngCurrent.Value)
            clnHeader.Add rngCurrent.Value, CStr(lngColumnCounter)
            lngColumnCounter = lngColumnCounter + 1
            Set rngCurrent = wsOriginal.Cells(lngRowCounterOriginal, lngColumnCounter)
        Loop
        
        'Reset Row Counter 
        lngRowCounterOriginal = 2
        lngRowCounterNormalized = 1
        lngColumnCounter = 1
        
        'Loop through the entire data set
        Do While Not IsEmpty(wsOriginal.Cells(lngRowCounterOriginal, lngColumnCounter))
        
            Set rngCurrent = wsOriginal.Cells(lngRowCounterOriginal, lngColumnCounter)
            strKey = rngCurrent.Value ' Get the key value from the current cell'
            lngColumnCounter = 1
        
            Do While Not IsEmpty(wsOriginal.Cells(lngRowCounterOriginal, lngColumnCounter))
                Set rngCurrent = wsOriginal.Cells(lngRowCounterOriginal, lngColumnCounter)
                    If rngCurrent.Value = "NULL" Then
                        'Skip it'
                    Else
                        'Add this item to the normalized sheet'
                        wsNormalized.Range("A" & lngRowCounterNormalized).Value = strKey
                        wsNormalized.Range("B" & lngRowCounterNormalized).Value = clnHeader(CStr(lngColumnCounter))
                        wsNormalized.Range("C" & lngRowCounterNormalized).Value = rngCurrent.Value
                        lngRowCounterNormalized = lngRowCounterNormalized + 1
                    End If
                
                lngColumnCounter = lngColumnCounter + 1
            Loop
            lngRowCounterOriginal = lngRowCounterOriginal + 1
            lngColumnCounter = 1    'We reset the column counter here because we're on a new row'
        Loop
    
    
    End Sub
    Thanking you in advance

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hi meljunk. seeing the workbook will help us help you. can you post your workbook with sample data, including the "after macro" scenario.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I suspect this can be done from the source data of the pivot table with a different pivot table design, but that may depend on what that data is.
    While a picture is great, an actual workbook would be even better, especially one with sample source data for the pivot table(s).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5

Posting Permissions

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