vba 将当前工作簿分配给 Excel 中的工作簿对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12858052/
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-11 18:06:08  来源:igfitidea点击:

Assign current WorkBook to Workbook object in Excel

excelexcel-vbaexcel-2010vba

提问by aSystemOverload

I've tried to assign the current workbook to a workbook object:

我试图将当前工作簿分配给工作簿对象:

Dim wb As Workbook, ws As Worksheet, rng As Range

wb = ThisWorkbook

In this instance, the code is running in and is the currently active workbook. But I get the following error, where am I going wrong:

在这种情况下,代码正在运行并且是当前活动的工作簿。但是我收到以下错误,我哪里出错了:

91 - Object variable or With block variable not set

91 - Object variable or With block variable not set

回答by CaBieberach

When assigning an object to a variable you have to use Set

将对象分配给变量时,您必须使用 Set

Set wb =ThisWorkbook