我希望将它从一个数据库更改为另一个数据库。
似乎在数据透视表上下文菜单中没有任何选项可以执行此操作。
我希望将它从一个数据库更改为另一个数据库。
似乎在数据透视表上下文菜单中没有任何选项可以执行此操作。
刚刚找到了方法-在表格的任何位置单击,然后转到页面顶部的选项卡并选择“选项”-从那里您将看到“更改数据源”选项。
看起来这很大程度上取决于你使用的Excel版本。我正在使用2007版本,当你右键单击表格时没有向导选项。你需要点击数据透视表才能在屏幕顶部的其他选项卡右侧出现额外的“数据透视表工具”。点击这里出现的“选项”选项卡,然后在功能区中央有一个名为“更改数据源”的大图标。
在Excel中,右键单击数据透视表
选择“向导”
点击“后退”
点击“获取数据...”
在查询窗口中
文件 - 表定义
然后您可以创建一个新的连接或选择不同的连接。
Sub SwapSources()
strOldSource = "2010 Data"
strNewSource = "2009 Data"
Dim tmpArrOut
For Each wsh In ThisWorkbook.Worksheets
For Each pvt In wsh.PivotTables
tmpArrIn = pvt.SourceData
' row 1 of SourceData is the connection string.
' rows 2+ are the SQL code broken down into 255-byte chunks.
' we need to concatenate rows 2+, replace, and then split them up again
strSource1 = tmpArrIn(LBound(tmpArrIn))
strSource2 = ""
For ii = LBound(tmpArrIn) + 1 To UBound(tmpArrIn)
strSource2 = strSource2 & tmpArrIn(ii)
Next ii
strSource1 = Replace(strSource1, strOldSource, strNewSource)
strSource2 = Replace(strSource2, strOldSource, strNewSource)
ReDim tmpArrOut(1 To Int(Len(strSource2) / 255) + 2)
tmpArrOut(LBound(tmpArrOut)) = strSource1
For ii = LBound(tmpArrOut) + 1 To UBound(tmpArrOut)
tmpArrOut(ii) = Mid(strSource2, 255 * (ii - 2) + 1, 255)
Next ii
' if the replacement SQL is invalid, the PivotTable object will throw an error
Err.Clear
On Error Resume Next
pvt.SourceData = tmpArrOut
On Error GoTo 0
If Err.Number <> 0 Then
MsgBox "Problems changing SQL for table " & wsh.Name & "!" & pvt.Name
pvt.SourceData = tmpArrIn ' revert
ElseIf pvt.RefreshTable <> True Then
MsgBox "Problems refreshing table " & wsh.Name & "!" & pvt.Name
Else
' table is now refreshed
' need to ensure that the "display name" for each pivot option matches
' the actual value that will be fed to the database. It is possible for
' these to get out of sync.
For Each pvf In pvt.PivotFields
'pvf.Name = pvf.SourceName
If Not IsError(pvf.SourceName) Then ' a broken field may have no SourceName
mismatches = 0
For Each pvi In pvf.PivotItems
If pvi.Name <> pvi.SourceName Then
mismatches = mismatches + 1
pvi.Name = "_mismatch" & CStr(mismatches)
End If
Next pvi
If mismatches > 0 Then
For Each pvi In pvf.PivotItems
If pvi.Name <> pvi.SourceName Then
pvi.Name = pvi.SourceName
End If
Next
End If
End If
Next pvf
End If
Next pvt
Next wsh
End Sub
对于 MS Excel 2000 办公版本, 点击数据透视表 您会发现在选项卡上方有一个名为“数据透视表工具”的选项卡 - 点击它 您可以从“数据”选项卡更改数据源
如果是Excel 2007,您可以在选项菜单/更改数据源中更改数据源。