Help with Pivot Table Macro

SCPbrito

Board Regular
Joined
Aug 1, 2024
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I have this macro below im running to create a pivot table from raw data. The macro runs fine except when it comes to the pivot table is it not accurately displaying all the times. I just ran it and it stopped at 1PM even though i have data going all the way to 4pm



Rich (BB code):
Sub Macro4()
'
' Macro4 Macro
'

'
    Dim wsItem_Transaction As Worksheet
    Dim wsPivotTable As Worksheet
    Dim lastRow As Long
    Dim currentDate As Date
    Dim cutoffTime As Date
    Dim i As Long

    ' Set worksheet references
    Set wsItem_Transaction = ThisWorkbook.Sheets("Item_Transaction") ' Replace with your transaction sheet name
    Set wsPivotTable = ThisWorkbook.Sheets("Pivot_Table")

    ' Step 1: Format column E in the transaction worksheet
    wsItem_Transaction.Columns("E:E").NumberFormat = "[$-en-US]m/d/yy h:mm AM/PM;@"

    ' Step 2: Delete rows with times before 7:00 AM in column E
    ' Get the last row with data in column E
    lastRow = wsItem_Transaction.Cells(wsItem_Transaction.Rows.Count, "E").End(xlUp).Row

    ' Set the cutoff time for 7:00 AM on the current day
    currentDate = Date
    cutoffTime = currentDate + TimeValue("07:00:00")

    ' Turn off screen updating for better performance
    Application.ScreenUpdating = False

    ' Loop through rows from bottom to top
    For i = lastRow To 1 Step -1
        ' Check if the cell in column E is a valid date
        If IsDate(wsItem_Transaction.Cells(i, "E").Value) Then
            ' Delete the row if the time is before 7:00 AM
            If wsItem_Transaction.Cells(i, "E").Value < cutoffTime Then
                wsItem_Transaction.Rows(i).Delete
            End If
        End If
    Next i

    ' Step 3: Clear all contents in the "Pivot Table" worksheet
    wsPivotTable.Cells.ClearContents

    ' Step 4: Create a new Pivot Table
    Application.CutCopyMode = False
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Item_Transaction!R1C1:R4709C30", Version:=8).CreatePivotTable _
        TableDestination:="'Pivot_Table'!R1C1", TableName:="PivotTable1", _
        DefaultVersion:=8

    ' Configure the Pivot Table
    wsPivotTable.Select
    Cells(1, 1).Select
    With ActiveSheet.PivotTables("PivotTable1")
        .ColumnGrand = True
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = True
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 1
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlCompactRow
    End With

    With ActiveSheet.PivotTables("PivotTable1").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With

    ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Time")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").PivotFields("Time").AutoGroup
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Quantity"), "Sum of Quantity", xlSum
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("User ID")
        .Orientation = xlRowField
        .Position = 1
    End With

    ' Restore screen updating
    Application.ScreenUpdating = True

    ' Notify the user that the process is complete
    MsgBox "Transaction sheet processed, and Pivot Table created.", vbInformation
End Sub
 

Attachments

  • 2024-12-05_16-19-15.png
    2024-12-05_16-19-15.png
    25.2 KB · Views: 1

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top