简体   繁体   English

在单元格中生成方法命令并在vba中运行

[英]build method command in cell and run it in vba

I have the following command which runs correctly if placed inside a macro in vba: 我有以下命令,如果将其放置在vba中的宏中,则该命令可以正确运行:

Set elements = doc.getElementsByClassName("media-body")

I try to have this command running while taking the information from within a cell. 我尝试从单元内获取信息时运行此命令。 Thus I place 因此,我放置

doc.getElementsByClassName("media-body")

in cell D5 , but when I run: Set elements = evaluate("Range(""D5"")" ) it throws an error (Run-time error '424': Object required. 在单元格D5中,但是当我运行: Set elements = evaluate("Range(""D5"")" )时,它将引发错误(运行时错误'424':必需对象。

Is there a way to solve it? 有办法解决吗?

No, there isn't a way to solve it. 不,没有办法解决。 Excel.Application.Evaluate only works with a subset of Excel objects. Excel.Application.Evaluate仅适用于一部分Excel对象。 From the documentation : 文档中

Remarks 备注

The following types of names in Microsoft Excel can be used with this method: Microsoft Excel中的以下类型的名称可以与此方法一起使用:

Formulas. 公式。 A1-style references. A1样式的引用。 You can use any reference to a single cell in A1-style notation. 您可以以A1样式表示法对单个单元格的任何引用。 All references are considered to be absolute references. 所有参考均被视为绝对参考。

Ranges. 范围。 You can use the range, intersect, and union operators (colon, space, and comma, respectively) with references. 您可以将range,intersect和union运算符(分别为冒号,空格和逗号)与引用一起使用。 Defined names. 定义的名称。 You can specify any name in the language of the macro. 您可以使用宏语言指定任何名称。

External references. 外部参考。 You can use the ! 您可以使用! operator to refer to a cell or to a name defined in another workbook ? 运算符引用一个单元格还是另一个工作簿中定义的名称? for example, Evaluate("[BOOK1.XLS]Sheet1!A1"). 例如,Evaluate(“ [BOOK1.XLS] Sheet1!A1”)。

Chart Objects. 图表对象。 You can specify any chart object name, such as "Legend", "Plot Area", or "Series 1", to access the properties and methods of that object. 您可以指定任何图表对象名称,例如“图例”,“绘图区域”或“系列1”,以访问该对象的属性和方法。 For example, Charts("Chart1").Evaluate("Legend").Font.Name returns the name of the font used in the legend. 例如,Charts(“ Chart1”)。Evaluate(“ Legend”)。Font.Name返回图例中使用的字体的名称。

The reason that you get an error 424 is because doc is an object reference in the code you are trying to run from the cell. 出现错误424的原因是, doc是您尝试从单元格运行的代码中的对象引用。 Evaluate has no clue what doc is , because there is no way to set it to an instance from within a cell. Evaluate有什么线索doc ,因为没有办法将它从一个小区内设置一个实例。

The only way to do anything meaningful with the cell contents in the question would be to parse the code in the cell and basically run it through an VBA-VBA interpreter. 对问题中的单元格内容进行有意义的处理的唯一方法是解析单元格中的代码,并基本上通过VBA-VBA解释器运行该代码。

EDIT 编辑

If I understand correctly what you're after (see the comments), what you'd really like is a way to generically search for HTML elements based on name and type. 如果我正确理解了您的要求(请参阅评论),那么您真正想要的是一种基于名称和类型来通用搜索HTML元素的方法。 I'd approach this by selecting how to search the element name based on a type parameter. 我将通过选择如何基于类型参数搜索元素名称来解决此问题。 The example below assumes that the target cell (in row sourceRow and column sourceCol ) contains the element name, ie "media-body", and the cell to the right contains its type - ie "ClassName". 下面的示例假定目标小区(在排sourceRow和列sourceCol )包含元素名称,即,“媒体体”,和单元向右包含它的类型-即“类名”。

Public Function GetElement(targetSheet As Worksheet, doc As HTMLDocument, _
                           sourceRow As Long, sourceCol As Long) As IHTMLElement
    With targetSheet
        'Get the element name from the passed cell.
        Dim elementName As String
        elementName = .Cells(sourceRow, sourceCol)
        'Get the element type from the adjacent cell.
        Dim elementType As String
        elementType = .Cells(sourceRow, sourceCol + 1)
        Select Case elementType
            Case "ClassName"
                Set GetElement = doc.getElementsByClassName(elementName)
            Case "Id"
                Set GetElement = doc.getElementById(elementName)
            Case "Name"
                Set GetElement = doc.getElementsByName(elementName)
            '...
        End Select
    End With
End Function

This could just as easily be accomplished with a comma delimited string in a single cell - something like "media-body,ClassName", or several other methods but this is the direction I'd go. 在单个单元格中用逗号分隔的字符串即可轻松完成此操作-类似于“ media-body,ClassName”或其他几种方法,但这就是我要去的方向。

Replace your: 更换:

Set elements = evaluate("Range(""D5"")")

With: 带有:

Set elements = Evaluate("D5")

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

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