VBA Pivot table for rosters help please

Adman1011

New Member
Joined
Feb 15, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Thank you for taking the time to help,

I am using VBA to create a pivot table that will be used to view staffing / allocations over a number of dates.
The data fields presently in the pivot table macro are separated in one column grouped in rows, Position 1 through to 4. "Actual work" is excluded

Date
workplace
shift time
and lastly info about the person on that shift ("Assignment Info")

:unsure:My first problem i am having is i cannot exclude the persons in the pivot field "Shifts" (Position 3) that get grouped as being "OFF" work that day from the table.

The second problem is this:
:unsure:I need to create a column in the pivot table "c" that gives a numerical count of the persons "assignment info" on shift that day at each workplace. i.e "1" in the column value
:unsure:I also need an additional column that gives a numerical count of the persons "on call" that day for each workplace. Note that the "shift" field has a sub grouping of "O/C"

Please find below a mocksheet (This is the raw data i receive) and my macro thus far to create a pivot table,

VBA Code:
Sub InsertPivotTable()

'Declare Variables
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long

'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Allocation.AssignedDuties_Brows")

'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="Pivot")

'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Pivot")

'Insert Row Fields
With ActiveSheet.PivotTables("Pivot").PivotFields("Duty Date")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("Owning Unit")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("Shift")
.Orientation = xlRowField
.Position = 3
'This is where i need to exclude the people as being rostered off that are grouped into "OFF"
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("Assignment Info")
.Orientation = xlRowField
.Position = 4

End With

'Insert Data Field
'This is where i need to count the number of "on call" in a seperate column "d" in the pivot table
'This is where i need to count the number of "assignment info" in this sub grouping of different shift times
'in a seperate column "c" in the pivot table
'Format Pivot Table
ActiveSheet.PivotTables("Pivot").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Pivot").TableStyle2 = "PivotStyleMedium9"

End Sub
 

Attachments

  • Mocksheet.PNG
    Mocksheet.PNG
    37.4 KB · Views: 22

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,215,734
Messages
6,126,547
Members
449,317
Latest member
chingiloum

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