2016-03-03 77 views
0

我是新來的編碼。我試圖執行下面的代碼,但excel總是彈出這兩個錯誤:VBA中的錯誤混淆

「運行時錯誤1004:object'_global的方法'範圍'失敗。」

「應用 - 定義或對象 - 定義的錯誤」。

我的代碼有什麼問題?

Sub CreatePivotTable() 
Dim PTCache As PivotCache 
Dim PT As PivotTable 
    Set PTCache = ActiveWorkbook.PivotCaches.Create(_ 
    SourceType:=xlDatabase, _ 
    SourceData:=Range(「A1」).CurrentRegion) 

Worksheets.Add 

Set PT = ActiveSheet.PivotTables.Add(_ 
PivotCache:=PTCache, _ 
TableDestination:=Range(「A3」)) 

With PT 
.PivotFields(「Region」).Orientation = xlPageField 
.PivotFields(「Month」).Orientation = xlColumnField 
.PivotFields(「SalesRep」).Orientation = xlRowField 
.PivotFields(「Sales」).Orientation = xlDataField 
.DisplayFieldCaptions = False 
End With 
End Sub 
+0

我回答這個昨天:看到這個答案】(http://stackoverflow.com/a/35742622/4240221) –

回答

1

它看起來像是「這是造成問題。與更換「使代碼運行得很好我的機器上:

'replaced 「 」 with " 
Sub CreatePivotTable() 
    Dim PTCache As PivotCache 
    Dim PT As PivotTable 
     Set PTCache = ActiveWorkbook.PivotCaches.Create(_ 
     SourceType:=xlDatabase, _ 
     SourceData:=Range("A1").CurrentRegion) 

    Worksheets.Add 

    Set PT = ActiveSheet.PivotTables.Add(_ 
    PivotCache:=PTCache, _ 
    TableDestination:=Range("A3")) 

    With PT 
     .PivotFields("Region").Orientation = xlPageField 
     .PivotFields("Month").Orientation = xlColumnField 
     .PivotFields("SalesRep").Orientation = xlRowField 
     .PivotFields("Sales").Orientation = xlDataField 
     .DisplayFieldCaptions = False 
    End With 
End Sub