简体   繁体   English

错误VBA 9下标超出范围

[英]Error VBA 9 Subscript out of range

I have an Excel workbook with 3 tabs. 我有一个带有3个选项卡的Excel工作簿。

Second tab (Sheet2) is with data from Oracle DB. 第二个选项卡(Sheet2)包含来自Oracle DB的数据。

I created a pivot table in Sheet3 with data from Sheet2. 我在Sheet3中使用Sheet2中的数据创建了一个数据透视表。

I want the Pivot Table to update automatically when the data changes. 我希望数据库表在数据更改时自动更新。

Here is the code: 这是代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("Sheet3").PivotTables("PivotTable4").PivotCache.Refresh  
End Sub

PivotTable4 is the name of the PivotTable. PivotTable4是数据透视表的名称。

After some minutes I got the error: 几分钟后我得到了错误:

Run-time error '9' : Subscript out of range. 运行时错误'9':下标超出范围。

When working with multiple workbooks, you'll get a "Subscript out of range" error if your code looks for an object with a particular name in one workbook when in fact it's in the other. 使用多个工作簿时,如果您的代码在一个工作簿中查找具有特定名称的对象,则实际上它位于另一个工作簿中时,您将收到“下标超出范围”错误。 Therefore, if you're going to have multiple workbooks open, it's always a good idea to qualify the one in which you want the code to be executed, for example: 因此,如果您要打开多个工作簿,那么限定您希望执行代码的工作簿总是一个好主意,例如:

Workbooks("MyWorkbook").Worksheets("Sheet3").PivotTables("Pi‌​votTable4").PivotCac‌​he.Refresh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM