VBA 对象不支持此属性或方法

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

VBA Object doesn't support this property or method

excelvba

提问by user2907249

I need to simply count the number of areas on a sheet. The code I have is :

我需要简单地计算一张纸上的区域数。我的代码是:

Sub areas()
   Dim i As Long
   i = Worksheets("Sheet2").Selection.Areas.Count
   MsgBox i
End Sub

But for some reason I get the error message "Object doesn't support this property or method." I have no idea why. This code was basically just copied from the Microsoft website.

但由于某种原因,我收到错误消息“对象不支持此属性或方法。” 我不知道为什么。这段代码基本上是从微软网站上复制过来的。

I can't even get the immediate window to print the Worksheets("Sheet2").Selection.Areas.Countportion.

我什至无法立即获得打印该Worksheets("Sheet2").Selection.Areas.Count部分的窗口。

Any quick help? I am using Excel 2010.

任何快速帮助?我正在使用 Excel 2010。

Thanks.

谢谢。

回答by

Object doesn't support this property or method.

对象不支持此属性或方法。

Think of it like if anything after the dot is called on an object. It's like a chain.

把它想象成在对象上调用点之后的任何东西。它就像一个链条。

An object is a class instance. A class instance supports some properties defined in that class type definition. It exposes whatever intelli-sense in VBE tells you (there are some hidden members but it's not related to this). So after each dot .you get intelli-sense (that white dropdown) trying to help you pick the correct action.

一个对象是一个类实例。类实例支持在该类类型定义中定义的一些属性。它暴露了 VBE 中告诉你的任何智能感知(有一些隐藏的成员,但与此无关)。因此,在每个点之后,.您都会得到智能感知(白色下拉菜单),试图帮助您选择正确的操作

(you can start either way - front to back or back to front, once you understand how this works you'll be able to identify where the problem occurs)

您可以从任何一种方式开始 - 从前到后或从后到前,一旦您了解这是如何工作的,您将能够确定问题发生的位置)

Type this much anywhere in your code area

在代码区域的任何地方输入这么多

Dim a As Worksheets
a.

you get help from VBE, it's a little dropdown called Intelli-sense

你从 VBE 那里得到帮助,它是一个名为 Intelli-sense 的小下拉菜单

enter image description here

在此处输入图片说明

It lists all available actionsthat particular object exposes to any user. You can't see the .Selectionmember of the Worksheets()class. That's what the error tells you exactly.

它列出了特定对象向任何用户公开的所有可用操作。你看不到班级的.Selection成员Worksheets()。这正是错误告诉你的。

Object doesn't support this property or method.

对象不支持此属性或方法。

If you look at the example on MSDN

如果您查看MSDN的示例

Worksheets("GRA").Activate
iAreaCount = Selection.Areas.Count

It activatesthe sheet first then calls the Selection...it's not connected together because Selectionis not a member of Worksheets()class. Simply, you can't prefixthe Selection

工作activates表首先调用Selection...它没有连接在一起,因为Selection它不是Worksheets()类的成员。简单地说,您不能前缀Selection

What about

关于什么

Sub DisplayColumnCount()
    Dim iAreaCount As Integer
    Dim i As Integer

    Worksheets("GRA").Activate
    iAreaCount = Selection.Areas.Count

    If iAreaCount <= 1 Then
        MsgBox "The selection contains " & Selection.Columns.Count & " columns."
    Else
        For i = 1 To iAreaCount
        MsgBox "Area " & i & " of the selection contains " & _
        Selection.Areas(i).Columns.Count & " columns."
        Next i
    End If
End Sub

from HERE

这里