按照你的需求,那红色字体处肯定应该有改动
请看实例:动态更改源数据表创建透视表.xls,代码不够简练。
Private Sub CommandButton1_Click()
sht = ListBox1.Value
Select Case sht
Case Is = "Sheet1"
sc = Sheets("sheet1").Range("C1:C8").Address
'判断工作簿中是否存在同名透视表
'On Error Resume Next
For j = 1 To ThisWorkbook.Sheets.Count
On Error Resume Next
For i = 1 To Sheets(j).PivotTables.Count
If Sheets(j).PivotTables(i).Name = "数据透视表1" Then
Sheets(j).Delete
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=sc).CreatePivotTable TableDestination:="", TableName:= _
"数据透视表1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
End If
Next
Next
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=sc).CreatePivotTable TableDestination:="", TableName:= _
"数据透视表1", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
Exit Sub
Case Is = "Sheet2"
sc = Sheets("sheet2").Range("C1:C8").Address
'判断工作簿中是否存在同名透视表
'On Error Resume Next
For j = 1 To ThisWorkbook.Sheets.Count
On Error Resume Next
For i = 1 To Sheets(j).PivotTables.Count
If Sheets(j).PivotTables(i).Name = "数据透视表2" Then
Sheets(j).Delete
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=sc).CreatePivotTable TableDestination:="", TableName:= _
"数据透视表2", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
End If
Next
Next
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=sc).CreatePivotTable TableDestination:="", TableName:= _
"数据透视表2", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
Exit Sub
Case Is = "Sheet3"
sc = Sheets("sheet3").Range("C1:C8").Address
'判断工作簿中是否存在同名透视表
'On Error Resume Next
For j = 1 To ThisWorkbook.Sheets.Count
On Error Resume Next
For i = 1 To Sheets(j).PivotTables.Count
If Sheets(j).PivotTables(i).Name = "数据透视表3" Then
Sheets(j).Delete
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=sc).CreatePivotTable TableDestination:="", TableName:= _
"数据透视表3", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
End If
Next
Next
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=sc).CreatePivotTable TableDestination:="", TableName:= _
"数据透视表3", DefaultVersion:=xlPivotTableVersion10
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
Exit Sub
End Select
End Sub
Pd59HqK8.rar
(15.91 KB, 下载次数: 34)
[此贴子已经被作者于2004-6-24 12:00:43编辑过] |