mable thomas
New Member
- Joined
- Aug 15, 2013
- Messages
- 3
Hi,
I get Run time error 13,Type mismatch while trying to run the below macro code.
I am actually trying to create a pivot table using macro.But I get the above error when I do so in the code line given in bold.Cud anyone help pls.. It's urgent.Pls note that there is around 85,000 rows of data in the dummy sheet.
Code is:
Public Sub CreatePivotTable()
Dim pt As PivotTable
Dim PtCache As PivotCache
Dim pageField1 As String
Dim rowField1 As String
Dim rowField2 As String
Dim colField As String
Dim dataField As String
Dim ws As Worksheet, ws1 As Worksheet
Dim Rng As Range
'delete pivot sheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Pivot Sheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
With Worksheets.Add
.Name = "Pivot Sheet"
End With
Set ws1 = Worksheets("Dummy")
ws1.Activate
lRow3 = ws1.Range("A" & ws.Rows.Count).End(xlUp).Row
pageField1 = ws1.Cells(1, 2).Value
rowField1 = ws1.Cells(1, 6).Value
colField1 = ws1.Cells(1, 6).Value
dataField = ws1.Cells(1, 14).Value
Worksheets("Dummy").Activate
ActiveSheet.Range("A1:V1" & lRow3).Select
Selection.Name = "Items"
'create pivot cache
Set PtCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=ws1.Range("Items")) selected contents of sheet
'create pivot table from cache
'PtCache.Refresh
Set pt = PtCache.CreatePivotTable(TableDestination:=Sheets("Pivot Sheet").Range("A3"), TableName:="Pivot Table")
PtCache.Refresh
'add fields
With pt
.PivotFields(rowField1).Orientation = xlRowField 'Op No
.PivotFields(colField1).Orientation = xlColumnField 'Defect
.PivotFields(dataField).Orientation = xlDataField 'sum of Tons
.PivotFields(pageField1).Orientation = xlPageField
End With
Worksheets("Pivot Sheet").Columns("A:DD").AutoFit
On Error Resume Next
Application.DisplayAlerts = False
On Error GoTo 0
Application.DisplayAlerts = True
Sheets("Pivot Sheet").Activate
Sheets("Pivot Sheet").Range("B6").Select 'selects a cell in the pivot table
Set ws1 = Worksheets("Pivot Sheet")
ws1.PivotTables("Pivot Table").ManualUpdate = True
With ws1.PivotTables("Pivot Table").DataBodyRange
.Offset(-1, -1).Copy ws1.Range("F1:F1")
End With
ws1.PivotTables("Pivot Table").ManualUpdate = False
End Sub
I get Run time error 13,Type mismatch while trying to run the below macro code.
I am actually trying to create a pivot table using macro.But I get the above error when I do so in the code line given in bold.Cud anyone help pls.. It's urgent.Pls note that there is around 85,000 rows of data in the dummy sheet.
Code is:
Public Sub CreatePivotTable()
Dim pt As PivotTable
Dim PtCache As PivotCache
Dim pageField1 As String
Dim rowField1 As String
Dim rowField2 As String
Dim colField As String
Dim dataField As String
Dim ws As Worksheet, ws1 As Worksheet
Dim Rng As Range
'delete pivot sheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Pivot Sheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
With Worksheets.Add
.Name = "Pivot Sheet"
End With
Set ws1 = Worksheets("Dummy")
ws1.Activate
lRow3 = ws1.Range("A" & ws.Rows.Count).End(xlUp).Row
pageField1 = ws1.Cells(1, 2).Value
rowField1 = ws1.Cells(1, 6).Value
colField1 = ws1.Cells(1, 6).Value
dataField = ws1.Cells(1, 14).Value
Worksheets("Dummy").Activate
ActiveSheet.Range("A1:V1" & lRow3).Select
Selection.Name = "Items"
'create pivot cache
Set PtCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:=ws1.Range("Items")) selected contents of sheet
'create pivot table from cache
'PtCache.Refresh
Set pt = PtCache.CreatePivotTable(TableDestination:=Sheets("Pivot Sheet").Range("A3"), TableName:="Pivot Table")
PtCache.Refresh
'add fields
With pt
.PivotFields(rowField1).Orientation = xlRowField 'Op No
.PivotFields(colField1).Orientation = xlColumnField 'Defect
.PivotFields(dataField).Orientation = xlDataField 'sum of Tons
.PivotFields(pageField1).Orientation = xlPageField
End With
Worksheets("Pivot Sheet").Columns("A:DD").AutoFit
On Error Resume Next
Application.DisplayAlerts = False
On Error GoTo 0
Application.DisplayAlerts = True
Sheets("Pivot Sheet").Activate
Sheets("Pivot Sheet").Range("B6").Select 'selects a cell in the pivot table
Set ws1 = Worksheets("Pivot Sheet")
ws1.PivotTables("Pivot Table").ManualUpdate = True
With ws1.PivotTables("Pivot Table").DataBodyRange
.Offset(-1, -1).Copy ws1.Range("F1:F1")
End With
ws1.PivotTables("Pivot Table").ManualUpdate = False
End Sub