使VBA宏在不同的工作簿上工作

时间:2018-03-16 14:07:43

标签: excel vba excel-vba dynamic

我是SO的新手,虽然我已经做了一些寻找答案的匿名搜索者。不幸的是,除非我错过了,否则我找不到具体问题的答案。有些人接近但不完全。所以这里。

我在下面发布的宏在家庭或本地工作簿中表现很好。这是一个非常具体的程序,可以处理来自不同来源的类似数据。我希望它可以在我提供数据的任何工作簿中工作。

我不确定这是否相关,但无论如何我会把它添加到想法中添加一些细节。

我们从不同的自动药物分配站提取数据,然后对其进行分类,以优化药站的药物可用性。因此,虽然数据通常相似,但每个工作站的每张纸的数据行范围是不同的。我认为正在发生的事情是该计划正在寻找原始范围,也许是表格。我想让它变得动态,所以我可以在任何工作簿中运行它。谢谢你的帮助。 下面是代码。调试屏幕突出显示

我假设存在阻止其运行的错误。我得到的错误是:

  

运行时错误'5':
  无效的过程调用或参数

谢谢大家

Sub Days_Unused()
'
' Days_Unused Macro
' Converts Hospital Wide Report Data to Pivot Table
'
' Keyboard Shortcut: Ctrl+d
'
    Range("A1").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Hospital_Wide_Inventory!R1C1:R671C15", Version:=xlPivotTableVersion15). 

        CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion15
    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("MedDescription")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Device")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField 
    ActiveSheet.PivotTables("PivotTable1").PivotFields("DaysUnused"), "Sum of 
    DaysUnused", xlSum
End Sub

1 个答案:

答案 0 :(得分:0)

请尝试以下代码,根据“Hospital_Wide_Inventory”表中的占用范围动态设置PivotTable

<强> 代码

Option Explicit

Sub Days_Unused()

' Converts Hospital Wide Report Data to Pivot Table
' Keyboard Shortcut: Ctrl+d

Dim WB1 As Workbook
Dim HospitalSht As Worksheet

Dim PvtSht As Worksheet
Dim PvtTbl As PivotTable
Dim PvtCache As PivotCache

Dim SrcRng As Range, SrcRngString As String
Dim LastRow As Long

Set WB1 = ActiveWorkbook
' add new sheet for Pivot-Table
Set PvtSht = WB1.Sheets.Add

' set "Hospital_Wide_Inventory" worksheet object
Set HospitalSht = WB1.Worksheets("Hospital_Wide_Inventory")

With HospitalSht
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column A

    ' set the Pivot source range object dynamicaly to the last row with data
    Set SrcRng = .Range(.Cells(1, 1), .Cells(LastRow, 15))

    SrcRngString = SrcRng.Address(False, False, xlA1, xlExternal) ' get the address of the range, including the sheet's name
End With

' set the Pivot Cache object
Set PvtCache = WB1.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcRngString)

' create the Pivot-Table object
Set PvtTbl = PvtSht.PivotTables.Add(PivotCache:=PvtCache, TableDestination:=PvtSht.Range("A3"), TableName:="PivotTable1")

' not set the Pivot-Fields
With PvtTbl
    With .PivotFields("MedDescription")
        .Orientation = xlRowField
        .Position = 1
    End With
    With .PivotFields("Device")
        .Orientation = xlColumnField
        .Position = 1
    End With

    ' add sum field
    .AddDataField .PivotFields("DaysUnused"), "Sum of DaysUnused", xlSum
End With

End Sub