+ Reply to Thread
Results 1 to 10 of 10

VBA alternative to a PIVOT table

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    VBA alternative to a PIVOT table

    Hi all,

    Needing help with data manipulation to show only the lines the represent the end result for an employees pay period.

    Brief contexts:
    An employee can be paid certain elements such as Ordinary hours, Leave hours, overtime etc.. Each row of data is made up of one of these elements.
    One row per element however employee can have multiple rows per payperiod.


    Where things get interesting is, employees can have additional retrospective lines, which are displayed as either a negative or positive.
    A negative means that it will negate the pay element where it was previously applied.
    A positive means that the value of the pay line is added to the element that was previously applied.

    If you are still following then great!, if not I totally understand.

    I have attached an example book of what I have done so far, which primarily consists of adding "DELETE" flags then delete the marked rows at the end, however this is
    a) far too slow, when i have over 300K rows
    b) not entirely the most robust solution.

    The reason this won't work in a PIVOT table, is where an employee has a negative retrospective line with a different rate of pay. The only options become showing the MAX of MIN rate.

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

    Re: VBA alternative to a PIVOT table

    Nothing attached
    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
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: VBA alternative to a PIVOT table

    Hi, Sorry now attached

    Also, i have commented out the "Delete" sub, so you can see what lines it is deleting
    Attached Files Attached Files

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA alternative to a PIVOT table

    Also, i have commented out the "Delete" sub
    Where?

    What are you actually wanting to achieve here...delete all rows with -(negative) values in Col R?
    What is the actual criteria...
    I suggest you explain the process...step by step
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  5. #5
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: VBA alternative to a PIVOT table

    I have made the code work, except it runs pretty slowly, about 1.5 seconds per each EMP_REF_unq statement.

    Any ideas on how to speed this up?

    here is the code:

    Sub Test_Wage_PIVOT_Alternitave()
    '============================================
    ' CODE WORKS as of 12.08.2020
    '============================================
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.AskToUpdateLinks = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    ThisWorkbook.Date1904 = False
    ActiveWindow.View = xlNormalView
    
    
    Sheets("Multiwage_NEW").UsedRange.ClearContents
    Sheets("Temp_M").UsedRange.ClearContents
        
    With Worksheets("Ref")
        Dim LastRow_REF_EmpID As Long
        LastRow_REF_EmpID = .Cells(.Rows.Count, "C").End(xlUp).Row
    End With
    
    
    For Each EMP_REF_unq In Sheets("Ref").Range("C2:C" & LastRow_REF_EmpID)
    
        Sheets("Temp_M").UsedRange.ClearContents
        Dim LastRow_Multiwage As Long, i As Long, j As Long
        
        With Worksheets("Multiwage")
           LastRow_Multiwage = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
        
        With Worksheets("Temp_M")
           j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        End With
        
        Sheets("Temp_M").Range("A1:X1").Value = Sheets("Multiwage").Range("A1:X1").Value
        Sheets("Multiwage_NEW").Range("A1:X1").Value = Sheets("Multiwage").Range("A1:X1").Value
    
        For i = 1 To LastRow_Multiwage
            With Worksheets("Multiwage")
                If .Cells(i, 1).Value = EMP_REF_unq Then
                    .Rows(i).Copy Destination:=Worksheets("Temp_M").Range("A" & j)
                    j = j + 1
                End If
            End With
        Next i
    
        Dim LastRow_TEMP_M As Long
        With Worksheets("Temp_M")
            LastRow_TEMP_M = .Cells(.Rows.Count, "A").End(xlUp).Row
        End With
    
        Sheets("Temp_M").Range("A1").Sort Key1:=Sheets("Temp_M").Columns("K"), Header:=xlYes
        
        For Each EMP_REF_For_WageLine In Sheets("Temp_M").Range("A2:A" & LastRow_TEMP_M)
        With Sheets("Temp_M").Range("A2:A" & j)
    
                
                Dim SEARCH_VALUE_WageType As String, SEARCH_VALUE_WageType_HOURS As String, SEARCH_VALUE_WageType_PayScaleGroup As String, START_ROW_TO_SEARCH_VALUE_WageType As String
                
                SEARCH_VALUE_WageType = EMP_REF_For_WageLine.Offset(0, 23)
                SEARCH_VALUE_WageType_HOURS = EMP_REF_For_WageLine.Offset(0, 15)
                SEARCH_VALUE_WageType_PayScaleGroup = EMP_REF_For_WageLine.Offset(0, 13)
                
                SEARCH_VALUE_WageType_address = EMP_REF_For_WageLine.Offset(0, 23).Address
                SEARCH_VALUE_WageType_Row = Split(SEARCH_VALUE_WageType_address, "$")(2)
                START_ROW_TO_SEARCH_VALUE_WageType = SEARCH_VALUE_WageType_Row + 1
                
                Dim FindString As String
                Dim Rng As Range
                FindString = SEARCH_VALUE_WageType
                If Trim(FindString) <> "" Then
                    With Sheets("Temp_M").Range("X" & START_ROW_TO_SEARCH_VALUE_WageType & ":X" & j + 1)
                        Set Rng = .Find(What:=FindString, _
                                        After:=.Cells(.Cells.Count), _
                                        LookIn:=xlValues, _
                                        LookAt:=xlWhole, _
                                        SearchOrder:=xlByRows, _
                                        SearchDirection:=xlNext, _
                                        MatchCase:=False)
                        If Not Rng Is Nothing Then
                            '*****Code for when wagetype is found
                            EMP_REF_For_WageLine.Offset(0, 15) = SEARCH_VALUE_WageType_HOURS + Rng.Offset(0, -8)
                            Rng.Offset(0, 1) = "DELETE"
                            Rng = ""
                            Rng.Offset(0, -8) = ""
                        Else
                        End If
                    End With
                End If
        End With
    
        Next EMP_REF_For_WageLine
        
        Delete.Remove_Superseded
        Delete.Remove_Zero
        
        Dim copySheet As Worksheet
        Dim pasteSheet As Worksheet
        
        Dim LastRow_TEMP_M2 As Long
        
        LastRow_TEMP_M2 = Sheets("Ref").Cells(Rows.Count, "C").End(xlUp).Row
        
        Set copySheet = Worksheets("Temp_M")
        Set pasteSheet = Worksheets("Multiwage_NEW")
        
        copySheet.Range("A2:X" & j).Copy
        pasteSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        
        
        Count = Count + 1
        Application.StatusBar = Count & " out of " & LastRow_TEMP_M2 - 1 & " completed."
    
    
    Next EMP_REF_unq
    
    Application.StatusBar = False
       
    Application.StatusBar = "Done!"
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.AskToUpdateLinks = True
    Application.DisplayAlerts = True
    Application.Calculation = xlAutomatic
    ThisWorkbook.Date1904 = False
    Application.StatusBar = False
    End Sub

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA alternative to a PIVOT table

    Well...as you have not answered any of post 4 questions...And your code is missing functions...
    Not possible to understand what you are actually wanting to achieve...
    Last edited by Sintek; 08-12-2020 at 02:41 AM.

  7. #7
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: VBA alternative to a PIVOT table

    Sorry sintek

    The initial objective was to delete rows that have been superseded by another row (This happens when there is a wage element such as ORD that is a negative value of equal amount of hours.
    The code i posted is the modified version that will search for the wage element and add or subtract hours based on the found results (if it is positive or neg) then remove the found lines (if any found)

    To summarize in steps:
    1. copies the pay period into a temp sheet from the master file for an individual employee
    2. for each row in the temp sheet, search the wage element (ord hours, overtime etc...)
    3. if results are found (more than the value that starts the search), then add the hours associated to that found value to the search value hours. Then delete the hours from the result element and add the text "DELETE" to the end column (this is for the deletion of the not useful rows at the end)
    4. if not found, then do nothing (this means there was no superseding entry for that wage element)
    5. when all wage elements are looked through then run the "Delete" function which is not listed in the code
    6. copy results to a different sheet
    7. Go to the next loops to the top again.


    Hopefully this covers it! let me know if i need to clarify something

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA alternative to a PIVOT table

    I see unnecessary steps...If the idea is to delete specific rows based on criteria then why not just have one action to do everything...Why copy to temp sheets and manipulate data etc etc...

    Is the idea to loop unique values in MultiWage Tab ... do some checks and delete rows for that unique reference... If so...
    What are the criteria or calculations that one needs to check...

    This happens when there is a wage element such as ORD that is a negative value of equal amount of hours.
    According to the above quote...Multiwage Tab has no such criteria...or am I missing something...

    Edit...

    In row 8 and 3... do these two cancel each other out...meaning delete them both or only the negative figure...?
    In row 9 and 4... do these two cancel each other out...meaning delete them both or only the negative figure...?
    Last edited by Sintek; 08-12-2020 at 03:47 AM.

  9. #9
    Forum Contributor
    Join Date
    06-24-2013
    Location
    New Zealand
    MS-Off Ver
    Excel 2016
    Posts
    124

    Re: VBA alternative to a PIVOT table

    The only real purpose of the temp sheet, is so i can see pause break the code while writing it to compare the output with with original. if the process of the temp sheet really adds to the time of the code running, i can remove it.

    an example of how it should work...

    Row 2 is the first entry of ORD (Ordinary hours) of 16.
    Row 7 is a negative of the same wage type description so will deduct two hours from Row 2
    Row 19 is a negative for a different period so will not affect the Row 2.
    The end result is Row 2 to having 14 hours

    I forgot to mention, to make the code work in the original book that I uploaded, you will need to put this formula in Col X which acts as a helper column

    =O2&"_"&N2&"_"&M2
    In summary, the code looks for the value in Col X and if found, then adds the hours to the row the search was based off. so if the hours added are neg or pos, it will still calculate correctly.

    Hope this makes more sense, sorry for the ambiguous postings

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: VBA alternative to a PIVOT table

    Row 7 is a negative of the same wage type description so will deduct two hours from Row 2
    deduct 2 hours from time...What about deduct R100 from Amount and then does this row 7 get deleted?

    Do rows 3 and 8 cancel each other out?
    Do rows 4 and 9 cancel each other out?

    I suggest uploading a sample file with a before and after expected scenario so that members can see exactly what you are wanting to achieve...
    Good luck...

    Untitled.png
    Last edited by Sintek; 08-12-2020 at 06:19 AM.

+ 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. Alternative to Pivot table
    By joshuar in forum Excel General
    Replies: 1
    Last Post: 06-15-2016, 05:00 AM
  2. Alternative to a Pivot table to create a list
    By simbalyon303 in forum Excel General
    Replies: 15
    Last Post: 01-09-2015, 06:34 PM
  3. Pivot table alternative
    By nick2price in forum Excel General
    Replies: 4
    Last Post: 08-16-2013, 06:12 AM
  4. [SOLVED] Alternative for Pivot table - row data
    By mmor79 in forum Excel General
    Replies: 2
    Last Post: 04-04-2012, 02:15 PM
  5. [SOLVED] Alternative for Pivot table - row data
    By mmor79 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2012, 07:35 PM
  6. Alternative need for Pivot Table
    By ronanm in forum Excel General
    Replies: 2
    Last Post: 01-21-2011, 11:06 AM
  7. Alternative to Pivot Table
    By papaexcel in forum Excel General
    Replies: 16
    Last Post: 07-01-2009, 04:18 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