excel VBA错误:CreatePivotTable /调用的对象已与其客户端断开连接

时间:2017-03-15 11:10:39

标签: excel-vba vba excel

我查了几个论坛来查找我的问题,但提供的解决方案似乎与我的代码不符。

我创建了以下代码,这很常见(在许多宏中使用它,从来没有像这样的问题)。我在另一个中称这个子。声明所有变量(一些在公共场合通过不同的潜艇使用)。它最终会起作用,但在它给出结果之前,它会在4-5分钟内出现崩溃。

崩溃线就是这个:

Set pvt = pvtCache.CreatePivotTable( _
                   TableDestination:=StartPvt, _
                   TableName:="PivotTable10")`

我无法确定原因。唯一的问题是,当我要求恢复时,它会给我以下信息:

  

调用的对象已与其客户端断开连接

代码

Option Explicit
Sub point23()

Dim a, i As Long
Dim dict As Dictionary
Dim wb1 As Workbook, ws1 As Worksheet, wb As Workbook, ws As Worksheet, ws2 

As Worksheet, ws3 As Worksheet
Dim FolderPath1 As String, filename1 As String, FolderPath As String,     FileName As String,
Dim FC As Integer, FCC As Integer, LC As Integer, LR As Integer
Dim pvtCache As PivotCache, pvt As PivotTable
Dim StartPvt As String, SrcData As Range, pf As String, pf_Name As String

Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.AskToUpdateLinks = False

FolderPath = ...
FileName = ...

FolderPath1 = ....
filename1 = ....

Set wb = Workbooks.Open(FolderPath & FileName)
wb.Sheets(1).Copy Before:=Sheets(1)
Set ws = wb.Worksheets(1)

ws.Cells.UnMerge

For i = 1 To 20
For j = 1 To 20
If ws.Cells(i, j).Value = "X" Then
FR = ws.Cells(i, j).Row
j = 20
i = 20
End If
Next j
Next i

ws.Rows("1").Resize(FR - 1).EntireRow.Delete

LC = ws.Cells(45, Columns.Count).End(xlToLeft).Column
LR = ws.Range("A" & Rows.Count).End(xlUp).Row

Set wb1 = Workbooks.Open(FolderPath1 & filename1)
Set ws1 = wb1.Worksheets(1)

For i = 1 To 20

If ws1.Cells(1, i).Value = "X" Then
FC = ws1.Cells(1, i).Column
Exit For
End If
Next i

For i = 1 To 20
If ws1.Cells(1, i).Value = "X" Then
FCC = ws1.Cells(1, i).Column
Exit For
End If
Next i

Set dict = New Dictionary

'使用词典执行Vlookup

a = ws1.Range("a1").CurrentRegion.Resize(, FC).Value
' Fill the dictionary
With dict
For i = 2 To UBound(a, 1)
.Item(a(i, FCC)) = a(i, FC)
Next

'做循环

ws.Range("a1").EntireColumn.Insert
a = ws.Range("A1").CurrentRegion.Resize(, FCC).Value
For i = 2 To UBound(a, 1)
If .Exists(a(i, 2)) Then
ws.Cells(i, 1).Value = .Item(a(i, 2))
Else: ws.Cells(i, 1).Value = "#N\A"
End If

Next
End With
dict.RemoveAll

wb1.Close

With ws.Range("A1")
.Value = "X"
.Resize(, 2).EntireColumn.Insert
End With

For i = 1 To LR
If ws.Cells(1, i).Value = vbNullString Then
ws.Cells(1, i).Value = "X"
End If
Next i

For i = 2 To LR
With ws
    .Cells(1, 1).Value = "X"
    .Cells(i, 1).Value = Cells(i, 3).Value & Cells(i, 12).Value
    .Cells(i, 2).Value = Cells(i, 4).Value & Cells(i, 6).Value
End With
Next i

'创建数据透视表

wb.Worksheets.Add After:=wb.Worksheets(1)
Set ws2 = wb.Sheets(2)


  ws.Activate
  Set SrcData = ws.Range("A2").CurrentRegion
  ws2.Activate

  Set pvtCache = wb.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)

Set pvt = ws2.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=ws2.Range("A1"), TableName:="PivotTable10")

pvt.PivotFields("X").Orientation = xlRowField
pvt.PivotFields("X").Orientation = xlRowField
pvt.PivotFields("X").Orientation = xlRowField

'create columns of the pivot
pf = "X"
pf_Name = "Sum " & pf
pvt.AddDataField pvt.PivotFields(pf), pf_Name, xlSum

With ws2.PivotTables("PivotTable10")
        .InGridDropZones = True
        .RowAxisLayout xlTabularRow
End With

With ws2
    .PivotTables("PivotTable10").PivotFields("Xl").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    .PivotTables("PivotTable10").PivotFields("X").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    .PivotTables("PivotTable10").PivotFields("X").Subtotals = _
        Array(False, False, False, False, False, False, False, False, False, False, False, False)
    .PivotTables("PivotTable10").RepeatAllLabels xlRepeatLabels
    .PivotTables("PivotTable10").ColumnGrand = False
End With

ws2.Range("A1").CurrentRegion.Copy
wb.Worksheets.Add After:=wb.Worksheets(2)
Set ws3 = wb.Worksheets(3)
With ws3
    .Name = "Sum X"
    .Range("A1").PasteSpecial xlPasteValues
End With

ws3.Rows.AutoFit

ws.Delete
wb.Save
wb.Close

Application.DisplayAlerts = True
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = True
Application.AskToUpdateLinks = True
End Sub

2 个答案:

答案 0 :(得分:1)

使用以下行更改创建新PivotTable的行:

' create a new Pivot Table in ws2 sheet, start from Cell A1
Set pvt = ws2.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=StartPvt, TableName:="PivotTable10")

此外,您可以从Sub

中删除以下代码行
StartPvt = ws2.Name & "!" & ws2.Range("A1").Address(ReferenceStyle:=xlR1C1)

并将Set pvt行替换为:

Set pvt = ws2.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=ws2.Range("A1"), TableName:="PivotTable10")

答案 1 :(得分:0)

对于有共同问题的人,我解决了将数据透视缓存创建和数据透视表创建重新组合成一行的问题:

 E/Twitter: Failed to get app auth token
                                                           com.twitter.sdk.android.core.TwitterApiException: HTTP request failed, Status: 403
                                                               at com.twitter.sdk.android.core.Callback.onResponse(Callback.java:42)
                                                               at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
                                                               at android.os.Handler.handleCallback(Handler.java:751)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at android.os.Looper.loop(Looper.java:154)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6209)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Authorization completed with an error
                                                           com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
                                                               at com.twitter.sdk.android.core.identity.AuthHandler.handleOnActivityResult(AuthHandler.java:98)
                                                               at com.twitter.sdk.android.core.identity.TwitterAuthClient.onActivityResult(TwitterAuthClient.java:161)
                                                               at com.twitter.sdk.android.core.identity.TwitterLoginButton.onActivityResult(TwitterLoginButton.java:131)
                                                               at in.crazyfingers.cyny.social_login.ActivitySocialSignIn.onActivityResult(ActivitySocialSignIn.java:305)
                                                               at android.app.Activity.dispatchActivityResult(Activity.java:7007)
                                                               at android.app.ActivityThread.deliverResults(ActivityThread.java:4173)
                                                               at android.app.ActivityThread.handleSendResult(ActivityThread.java:4220)
                                                               at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1559)
                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                               at android.os.Looper.loop(Looper.java:154)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6209)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)