尝试设置范围对象时键入不匹配错误

时间:2016-12-22 21:50:51

标签: excel vba excel-vba mismatch

Sub pivott()

Dim wbcon As Workbook
Dim WSD As Worksheet, shHard As Worksheet, shSnow As Worksheet
Dim PTCache As PivotCache
Dim PT As pivottable
Dim PRange As Range
Dim FinalRow As Long, FinalCol As Long
Dim clt As String

Set wbcon = ThisWorkbook
Set shSnow = wbcon.Worksheets("SNOW Raw")

p = wbcon.Sheets("Raw Data").Cells(3, 2)

If sheetexist(p & " Pivot") = False Then
        wbcon.Sheets.Add After:=wbcon.Sheets("Pivot Table")
        ActiveSheet.Name = p & " Pivot"
End If

    Set WSD = wbcon.Worksheets(p & " Pivot")
    For Each PT In WSD.PivotTables
        PT.TableRange2.Clear
    Next PT

    FinalRow = shSnow.Cells(Rows.Count, 1).End(xlUp).Row
    FinalCol = shSnow.Cells(1, Columns.Count).End(xlToLeft).Column


    Set PRange = wbcon.Sheets("SNOW Raw").Range(wbcon.cells(1,1),wbcon.cells(FinalRow, FinalCol))

    Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=PRange)

    Set PT = PTCache.CreatePivotTable(TableDestination:=WSD.Cells(1, 1), TableName:=p & " Pivot Table")

    PT.ManualUpdate = True


    PT.AddFields RowFields:=Array("Opened Months", "Problem Number"), ColumnFields:="Priority"

    With PT.PivotFields("Description")
        .Orientation = xlDataField
        .Function = xlCount
        .Position = 1
    End With
    PT.ManualUpdate = False
End Sub`

我道歉,只上传了不匹配错误的部分。

sheetexist是一个检查工作表名称的函数。

我在PRange上遇到类型不匹配错误,我尝试将Range更改为Range("A1").currentregion但我仍然得到相同的错误。我该如何解决这个问题?

0 个答案:

没有答案