excel - 工作表不起作用

标签 excel vba

我正在尝试编写一个 Excel Visual Basic 宏。

我的问题是这段代码有效:

Dim x As String
x = Worksheets("Abgabe").Cells(20, 3).Value

但这不是:

Dim y As Worksheet
y = Worksheets("Abgabe")

此外,如果我使用 ActiveWorkbook,代码将无法工作。

Dim y As Worksheet
y = ActiveWorkbook.Worksheets("Abgabe")

我收到此错误:

Object variable or With block variable not set

可能是什么问题?

最佳答案

Dim y As Worksheet

y = Worksheets("Abgabe")

使用这个(您必须使用Set)

Dim y As Worksheet
Set y = Worksheets("Abgabe")

来自 MSDN (http://msdn.microsoft.com/en-us/library/aa192490.aspx):

Set Keyword: In VBA, the Set keyword is necessary to distinguish between 
assignment of an object and assignment of the default property of the object.

关于excel - 工作表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10196620/

相关文章:

excel - 使用网络路径从 vba 运行 Python 脚本

excel - VBA - 选择方法失败

vba - 是否可以从批处理文件将错误代码返回到 VBA?

c# - 将 Excel 工作表中的数据读取到 C# 中没有空行的数据表中

excel - VBA 找不到我的工作表名称

excel - 如何按月份名称过滤excel列?

excel - VBA 中多个 ComboBox 控件的单个事件处理程序

VBA - 在公式中使用 Columns()

java - 如何从 Excel VBA 运行 Java Eclipse?

excel - 在excel上具有多个条件的平均值