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