I have the following code:
Dim StartCell3 As Range
Dim DataRange3 As Range
Set StartCell3 = Range("A1")
Set DataRange3 = StartCell3.CurrentRegion
DataRange3.Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=DataRange3, Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Stocks!R1C6", TableName:="PivotTable8", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Stocks").Select
Cells(1, 6).Select
With ActiveSheet.PivotTables("PivotTable8").PivotFields("Material")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable8").AddDataField ActiveSheet.PivotTables( _
"PivotTable8").PivotFields("Unrestricted"), "Sum of Unrestricted", xlSum
Range("F6").Select
With ActiveSheet.PivotTables("PivotTable8")
.ColumnGrand = False
.RowGrand = False
End With
When it comes to Sheets("Stocks").select, it gives subscript out of range error. It does not give any error while working on a module, but I created a macro enabled workbook and inserted a button that assigned with this macro.
The main problem is, from my created macro, the working code is on this workbooks code section as normally. Therefore, it only has sheet1, does not have sheet2 or some other sheet name. Why it gives an error? Why the workbook can not recognizes the other workbook?