简体   繁体   English

Excel VBA 宏:未定义用户定义的类型

[英]Excel VBA Macro: User Defined Type Not Defined

I'm getting the above error when trying to execute this macros.尝试执行此宏时出现上述错误。 I'm pretty new to Macros and coding in general so please forgive the ignorance.我对宏和一般编码很陌生,所以请原谅我的无知。

Sub DeleteEmptyRows()

Dim oTable As Table, oRow As Row, _
TextInRow As Boolean, i As Long

Application.ScreenUpdating = False

For Each oTable In ActiveDocument.Tables
    For Each oRow In oTable.Rows

        TextInRow = False

        For i = 2 To oRow.Cells.Count
            If Len(oRow.Cells(i).Range.Text) > 2 Then
                'end of cell marker is actually 2 characters
                TextInRow = True
                Exit For
            End If
        Next

        If TextInRow = False Then
            oRow.Delete
        End If
    Next
Next
Application.ScreenUpdating = True

End Sub

Your error is caused by these:您的错误是由以下原因引起的:

Dim oTable As Table, oRow As Row,

These types, Table and Row are not variable types native to Excel.这些类型TableRow不是 Excel 固有的变量类型。 You can resolve this in one of two ways:您可以通过以下两种方式之一解决此问题:

  1. Include a reference to the Microsoft Word object model.包括对 Microsoft Word 对象模型的引用。 Do this from Tools |从工具中执行此操作 | References, then add reference to MS Word.引用,然后添加对 MS Word 的引用。 While not strictly necessary, you may like to fully qualify the objects like Dim oTable as Word.Table, oRow as Word.Row .虽然不是绝对必要,但您可能希望将Dim oTable as Word.Table, oRow as Word.Row等对象完全限定Dim oTable as Word.Table, oRow as Word.Row This is called early-binding.这称为早期绑定。在此处输入图片说明
  2. Alternatively, to use late-binding method, you must declare the objects as generic Object type: Dim oTable as Object, oRow as Object .或者,要使用后期绑定方法,您必须将对象声明为通用Object类型: Dim oTable as Object, oRow as Object With this method, you do not need to add the reference to Word, but you also lose the intellisense assistance in the VBE.使用此方法,您无需添加对 Word 的引用,但也会失去 VBE 中的智能感知帮助。

I have not tested your code but I suspect ActiveDocument won't work in Excel with method #2, unless you properly scope it to an instance of a Word.Application object.我没有测试过你的代码,但我怀疑ActiveDocument不能在 Excel 中使用方法 #2,除非你将它正确地限定到 Word.Application 对象的一个​​实例。 I don't see that anywhere in the code you have provided.我在您提供的代码中的任何地方都没有看到。 An example would be like:一个例子是这样的:

Sub DeleteEmptyRows()
Dim wdApp as Object
Dim oTable As Object, As Object, _
TextInRow As Boolean, i As Long

Set wdApp = GetObject(,"Word.Application")

Application.ScreenUpdating = False

For Each oTable In wdApp.ActiveDocument.Tables

I am late for the party.我参加聚会迟到了。 Try replacing as below, mine worked perfectly- "DOMDocument" to "MSXML2.DOMDocument60" "XMLHTTP" to "MSXML2.XMLHTTP60"尝试替换如下,我的工作完美 - “DOMDocument”到“MSXML2.DOMDocument60”“XMLHTTP”到“MSXML2.XMLHTTP60”

Sub DeleteEmptyRows()  

    Worksheets("YourSheetName").Activate
    On Error Resume Next
    Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub

The following code will delete all rows on a sheet(YourSheetName) where the content of Column A is blank.以下代码将删除工作表 (YourSheetName) 上 A 列内容为空的所有行。

EDIT: User Defined Type Not Defined is caused by "oTable As Table" and "oRow As Row".编辑:未定义的用户定义类型是由“oTable As Table”和“oRow As Row”引起的。 Replace Table and Row with Object to resolve the error and make it compile.用 Object 替换 Table 和 Row 以解决错误并使其编译。

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

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