vba “未定义用户定义的类型”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24261557/
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
'User Defined Type Not Defined' error
提问by holdo1
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.
尝试执行此宏时出现上述错误。我对宏和编码很陌生,所以请原谅我的无知。
Thanks
谢谢
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
回答by David Zemens
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. You can resolve this in one of two ways:
这些类型的,Table
而Row
不是变量类型,原产于Excel中。您可以通过以下两种方式之一解决此问题:
- Include a reference to the Microsoft Word object model. Do this from Tools | References, then add reference to MS Word. While not strictly necessary, you maylike to fully qualify the objects like
Dim oTable as Word.Table, oRow as Word.Row
. This is called early-binding. - Alternatively, to use late-binding method, you must declare the objects as generic
Object
type: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.
- 包括对 Microsoft Word 对象模型的引用。从工具中执行此操作 | 引用,然后添加对 MS Word 的引用。虽然不是绝对必要的,但您可能希望完全限定对象,例如
Dim oTable as Word.Table, oRow as Word.Row
. 这称为早期绑定。 - 或者,要使用后期绑定方法,您必须将对象声明为泛型
Object
类型:Dim oTable as Object, oRow as Object
。使用此方法,您无需添加对 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. I don't see that anywhere in the code you have provided. An example would be like:
我尚未测试您的代码,但我怀疑ActiveDocument
使用方法 #2 在 Excel 中无法正常工作,除非您将其范围正确地限定为 Word.Application 对象的实例。我在您提供的代码中的任何地方都没有看到。一个例子是这样的:
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
回答by GideonMetre
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”
回答by Olle89
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". Replace Table and Row with Object to resolve the error and make it compile.
编辑:未定义的用户定义类型是由“oTable As Table”和“oRow As Row”引起的。用 Object 替换 Table 和 Row 以解决错误并使其编译。