[英]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.这些类型
Table
和Row
不是 Excel 固有的变量类型。 You can resolve this in one of two ways:您可以通过以下两种方式之一解决此问题:
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.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. 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.