the workbook: In my workbook the user completes a table with data. When clicking a command button the VBA code (see below) updates a PivotTable on another worksheet and then splits this table into different worksheets based on the different values filled in the pivotfield "Monstertype" (each pivotitem in that pivotfield becomes a new sheet with that table). These sheets are later transferred to a word document.
The problem: On the first trial the first pivotitem to encounter was called "Hygiene control", it then worked fine. On later trials it keeps looking for the pivotitem "Hygiene control" even if its not there anymore, with as a result I receive the error that it falls outside the subscript.
Sub exportPivot()
Application.ScreenUpdating = False
Worksheets("Blad9").PivotTables("Draaitabel17").RefreshTable
Dim wbbron As Workbook
Dim wbdoel As Workbook
Dim shtbron As Worksheet
Dim shtdoel As Worksheet
Dim pvt As PivotTable
Dim pi As PivotItem
Set wbbron = ActiveWorkbook
Set shtbron = wbbron.Sheets("Blad9")
Set pvt = shtbron.PivotTables("Draaitabel17")
pvt.ShowPages Pagefield:="Monstertype"
Set wbdoel = Workbooks.Add
For Each pi In pvt.PageFields("Monstertype").PivotItems
wbbron.Sheets(pi.Value).Move after:=wbdoel.Sheets(wbdoel.Sheets.Count)
Next pi
Application.DisplayAlerts = False
wbdoel.Sheets(1).Delete
Application.DisplayAlerts = True
VulOfferte wbdoel
End Sub
The error occurs on
wbbron.Sheets(pi.Value).Move after:=wbdoel.Sheets(wbdoel.Sheets.Count)
the cry for help: It drives me crazy, please help.