VBA 运行时错误 91。无法设置对象变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22840227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:04:49  来源:igfitidea点击:

VBA Run time error 91. Trouble setting object variable

excelvbaexcel-vbaruntime-error

提问by user3494029

I don't think the answer is too complicated, I must just be missing something basic. To give context, the code and this macro were written by an employee who was no longer working here when I started. I have just used it a number of times on a monthly basis and it suddenly gave me an error today when I attempted to run the VBA script.

我不认为答案太复杂,我一定只是遗漏了一些基本的东西。为了提供上下文,代码和这个宏是由我开始时不再在这里工作的员工编写的。我只是每月使用它多次,今天当我尝试运行 VBA 脚本时,它突然给了我一个错误。

It gives me the standard Run Time Error 91 (Object variable or block variable not set). The code specifically is:

它给了我标准Run Time Error 91 (Object variable or block variable not set)。代码具体是:

Dim DataBook As Workbook
Set DataBook = Workbooks.Open(Import_File_Path)
DataBook.Worksheets("Sheet1").Name = "Database"

    Application.DisplayAlerts = False
    Tool.Worksheets("Database").Delete      <-----highlighted error line
    Application.DisplayAlerts = True

DataBook.Worksheets("Database").Move before:=Tool.Worksheets(1)

The VBA script is attempting to take raw data from an Excel sheet that I have already generated, and import the file (thus that part in the Setline) and perform a number of calculations to create more data and stats from there. However the problem I feel lies in the section I posted here and the objects and variables.

VBA 脚本试图从我已经生成的 Excel 工作表中获取原始数据,并导入文件(因此该部分Set在行中)并执行大量计算以从那里创建更多数据和统计信息。然而,我觉得问题在于我在这里发布的部分以及对象和变量。

The highlighted error row is the one that says Tool.Worksheets("Database").Delete. When I added a Watch to that row the value states Object Variable or With block variable not set. This leads me to believe something is not being set as it should as an object, but again I am a VBA newbie and I am not confident about any of my gut feelings.

突出显示的错误行是说Tool.Worksheets("Database").Delete. 当我向该行添加 Watch 时,值状态Object Variable or With block variable not set. 这让我相信有些东西没有被设置为一个对象,但我又是一个 VBA 新手,我对我的任何直觉都没有信心。

For what its worth, I should mention I added another Watchto the line that says DataBook.Worksheets("Sheet1").Name = "Database"and the value for that states Subscript out of range. I thought that meant it could also be a problem, however I am 100% sure there is a tab in the file I am importing that is named Sheet1.

就其价值而言,我应该提到我Watch在行中添加了另一个内容DataBook.Worksheets("Sheet1").Name = "Database"和该状态的值Subscript out of range。我认为这意味着它也可能是一个问题,但是我 100% 确定我正在导入的文件中有一个名为Sheet1.

回答by Gary's Student

You need some lines of code that establish Toolas a workbook. Something like:

您需要一些将Tool建立为工作簿的代码行。就像是:

Dim Tool As Workbook
Set Tool = ActiveWorkbook

回答by GauravSingh

You can use

您可以使用

Dim DataBook As Workbook
Set DataBook = Workbooks.Open(Import_File_Path)

DataBook.Worksheets("Sheet1").Name = "Database"

Application.DisplayAlerts = False
DataBook.Worksheets("Database").Delete      <-----Change
Application.DisplayAlerts = True

DataBook.Worksheets("Database").Move before:=Tool.Worksheets(1)