我想将它从一个数据库更改为另一个。
数据透视表上下文菜单上似乎没有任何选项可以执行此操作
只需点击表格中的任意位置,然后转到页面顶部的选项卡并选择选项 - 从那里您将看到更改数据源选项。
看起来这在很大程度上取决于您的 Excel 版本。我使用的是 2007 版本,当您右键单击表格时,它没有提供向导选项。您需要单击数据透视表以使额外的“数据透视表工具”出现在屏幕顶部其他选项卡的右侧。单击此处显示的“选项”选项卡,然后功能区中间有一个名为“更改数据源”的大图标。
右键单击excel中的数据透视表选择向导单击“返回”单击查询窗口中的“获取数据...”文件 - 表定义
然后你可以创建一个新的或选择一个不同的连接
为了从 excel 2007 中的功能区更改数据源...
单击工作表中的数据透视表。转到显示数据透视表工具、选项选项卡的功能区。选择更改数据源按钮。将出现一个对话框。
要获得正确的范围并避免出现错误消息...选择现有字段的内容并将其删除,然后切换到新的数据源工作表并突出显示数据区域(对话框将保留在所有窗口的顶部)。正确选择新数据源后,它将填充对话框中的空白字段(之前已删除)。单击确定。切换回您的数据透视表,它应该已经使用来自新源的新数据进行了更新。
这个附加组件将为您解决问题。
http://www.contextures.com/xlPivotPlayPlus01.html
它显示连接字符串,并允许对其进行更改。如果需要,不要忘记更改查询 SQL(使用相同的工具)。
对任何不涉及从头开始重新创建数据透视表的解决方案要小心。您的数据透视字段的选项名称可能与它们呈现给数据库的值不同步。
例如,在我正在处理的一本涉及人口统计数据的工作簿中,如果您尝试选择“20-24”年龄段选项,Excel 实际上会为您提供 25-29 岁的数字。当然,它不会告诉你它正在这样做。
有关解决此问题的问题的程序化 (VBA) 方法,请参见下文。我认为它相当完整/强大,但我不经常使用数据透视表,因此希望得到反馈。
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
右键单击excel中的数据透视表选择向导单击“返回”单击查询窗口中的“获取数据...”文件 - 表定义
然后你可以创建一个新的或选择一个不同的连接
完美地工作。
获取数据按钮在小按钮旁边,在范围文本输入框旁边有一个红色箭头。
对于 MS excel 2000 office 版本,单击数据透视表,您将在功能区上方找到一个选项卡,称为 Pivottable 工具 - 单击该选项卡您可以从数据选项卡更改数据源
如果是 Excel 2007,您可以在选项菜单/更改数据源中更改数据源