Excel 弹出宏插件重复电子表格

Excel macro Add-in duplicate spreadsheet pop up

我的宏从一组数据转储中从头开始创建一个枢轴 table。我正在尝试将此宏移动到加载项。该加载项每次都处理新数据,但出于某种原因,它会弹出我的代码最初处理的第二个工作簿。

我已通读加载项网站以确保我正确设置了加载项。我的其他插件宏有效(只有 2 个。还在学习中)

Sub OpenAndHoldPivot()

    Dim sht As Worksheet
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String

'Determine the data range you want to pivot

    Dim finRow As String
    With ActiveWorkbook
    finRow = ActiveSheet.Range("A200000").End(xlUp).Row
    SrcData = ActiveSheet.Name & "!" & Range("A4:BO" & finRow - 1).Address  (ReferenceStyle:=xlR1C1)
    End With

'Create a new worksheet

    Set sht = Sheets.Add

'Pivot Table Start

    StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)

'Create Pivot Cache from Source Data

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

'Create Pivot table from Pivot Cache

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

'------------------------------------------------------------------------------


    Set pvt = ActiveSheet.PivotTables("PivotTable1")

'Add item to the Report Filter

    pvt.PivotFields("Future Fill Date").Orientation = xlPageField

'Add item to the Column Labels

    pvt.PivotFields("Worker Type").Orientation = xlColumnField

'Add item to the Row Labels

    pvt.PivotFields("Flex Division").Orientation = xlRowField

'Turn on Automatic updates/calculations --like screenupdating to speed up code

    pvt.ManualUpdate = False

'------------------------------------------------------------------------------


    ActiveSheet.Name = "Pivot"


'------------------------------------------------------------------------------

    Dim pf As String
    Dim pf_Name As String

    pf = "FT/PT"
    pf_Name = "Sum of FT/PT"

    Set pvt = ActiveSheet.PivotTables("PivotTable1")

    pvt.AddDataField pvt.PivotFields("FT/PT"), pf_Name, xlCount

'------------------------------------------------------------------------------

    Dim pm As PivotField

    Set pm = ActiveSheet.PivotTables("PivotTable1").PivotFields("Future Fill Date")

'Clear Out Any Previous Filtering

    pm.ClearAllFilters

'Filter on 2014 items

    pm.CurrentPage = "(blank)"
'------------------------------------------------------------------------------


    Sheets("Sheet1").Name = "Data"


End Sub

对我做错了什么有什么想法吗?

我真的认为您的问题出在您正在使用的工作簿或工作sheet 的一些不一致引用(或缺少引用)上。具体来说,我认为问题出在行

Set sht = Sheets.Add

由于您的 Sheets 引用未指定要添加新作品的工作簿sheet,它将默认为当前活动的工作簿,它可能是您的加载项工作簿。如果您更清楚自己想要哪些工作簿和工作sheet,您将对自己有很大帮助。为了使用您的示例说明这一点,您可以从

开始
Sub OpenAndHoldPivot()
    Dim workingWB As Workbook
    Dim workingWS As Worksheet
    Set workingWB = ActiveWorkbook
    Set workingWS = activeworksheet

    'Determine the data range you want to pivot
    Dim srcData As Range
    Dim srcDataText As String
    With workingWS
        Dim finRow As Long
        finRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set srcData = .Range("A4").Resize(finRow - 1, 67)
        srcDataText = .Name & "!" & srcData.Address(ReferenceStyle:=xlR1C1)
    End With

这清楚地确定了您的所有代码将运行哪个工作簿。此外,如果您查看我的 With 块并将其与您的进行比较,您会发现您在 Range 引用之前错过了一个 .,这可能会再次引用您的加载项或活动工作簿(您永远无法确定。

之后,我就继续往下代码...

    'Create a new worksheet in the working workbook
    Dim pivotWS As Worksheet
    Set pivotWS = workingWB.Sheets.Add

    'Pivot Table Start
    Dim StartPvtText As String
    StartPvtText = pivotWS.Name & "!" & pivotWS.Range("A3").Address(ReferenceStyle:=xlR1C1)

    'Create Pivot Cache from Source Data
    Dim pvtCache As PivotCache
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                   SourceType:=xlDatabase, _
                   SourceData:=srcDataText)

    'Create Pivot table from Pivot Cache
    Dim pvt As PivotTable
    Set pvt = pvtCache.CreatePivotTable( _
              TableDestination:=StartPvtText, _
              TableName:="PivotTable1")

另请注意,我将所有变量声明为尽可能靠近它们的使用位置。这使您更容易理解并确定您使用的是具有预期类型的​​正确变量。

进一步了解您的代码,您已经多次提到 ActiveSheet。将其替换为特定参考以保持一致。在我的代码中,我很少使用 ActiveSheetActiveCell。我试图在完整模块中修复下面的参考文献,但只有您才能判断这是否准确(因为不完全清楚您想要哪本书或 sheet)。

最后,还有最后一行代码 Sheets("Sheet1").Name = "Data"。我不知道应该参考哪个工作簿,但我猜应该是 workingWB.Sheets("Sheet1").Name = "Data".

Option Explicit

Sub OpenAndHoldPivot()
    Dim workingWB As Workbook
    Dim workingWS As Worksheet
    Set workingWB = ActiveWorkbook
    Set workingWS = activeworksheet

    'Determine the data range you want to pivot
    Dim srcData As Range
    Dim srcDataText As String
    With workingWS
        Dim finRow As Long
        finRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        Set srcData = .Range("A4").Resize(finRow - 1, 67)
        srcDataText = .Name & "!" & srcData.Address(ReferenceStyle:=xlR1C1)
    End With

    'Create a new worksheet in the working workbook
    Dim pivotWS As Worksheet
    Set pivotWS = workingWB.Sheets.Add

    'Pivot Table Start
    Dim StartPvtText As String
    StartPvtText = pivotWS.Name & "!" & pivotWS.Range("A3").Address(ReferenceStyle:=xlR1C1)

    'Create Pivot Cache from Source Data
    Dim pvtCache As PivotCache
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                   SourceType:=xlDatabase, _
                   SourceData:=srcDataText)

    'Create Pivot table from Pivot Cache
    Dim pvt As PivotTable
    Set pvt = pvtCache.CreatePivotTable( _
              TableDestination:=StartPvtText, _
              TableName:="PivotTable1")

    '------------------------------------------------------------------------------
    Set pvt = pivotWS.PivotTables("PivotTable1")

    'Add item to the Report Filter
    pvt.PivotFields("Future Fill Date").Orientation = xlPageField

    'Add item to the Column Labels
    pvt.PivotFields("Worker Type").Orientation = xlColumnField

    'Add item to the Row Labels
    pvt.PivotFields("Flex Division").Orientation = xlRowField

    'Turn on Automatic updates/calculations --like screenupdating to speed up code
    pvt.ManualUpdate = False

    '------------------------------------------------------------------------------
    pivotWS.Name = "Pivot"

    '------------------------------------------------------------------------------
    Dim pf As String
    Dim pf_Name As String
    pf = "FT/PT"
    pf_Name = "Sum of FT/PT"
    Set pvt = pivotWS.PivotTables("PivotTable1")
    pvt.AddDataField pvt.PivotFields("FT/PT"), pf_Name, xlCount

    '------------------------------------------------------------------------------
    Dim pm As PivotField
    Set pm = pivotWS.PivotTables("PivotTable1").PivotFields("Future Fill Date")

    'Clear Out Any Previous Filtering
    pm.ClearAllFilters

    'Filter on 2014 items
    pm.CurrentPage = "(blank)"

    '------------------------------------------------------------------------------
    workingWB.Sheets("Sheet1").Name = "Data"
End Sub