'用户定义的类型未定义'错误

时间:2021-07-14 07:55:30

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.

我在尝试执行此宏时遇到上述错误。我对Macros很新,一般编码所以请原谅无知。

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

2 个解决方案

#1


9  

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:

这些类型,表和行不是Excel原生的变量类型。您可以通过以下两种方式之一解决此问题:

  1. 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 may like to fully qualify the objects like Dim oTable as Word.Table, oRow as Word.Row. This is called early-binding. '用户定义的类型未定义'错误
  2. 包括对Microsoft Word对象模型的引用。从工具|执行此操作引用,然后添加对MS Word的引用。虽然不是绝对必要,但您可能希望将像Dim oTable这样的对象完全限定为Word.Table,oRow as Word.Row。这称为早期绑定。
  3. 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.
  4. 或者,要使用后期绑定方法,必须将对象声明为通用对象类型:Dim oTable as Object,oRow as Object。使用此方法,您不需要添加对Word的引用,但您也失去了VBE中的intellisense帮助。

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

#2


-1  

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”引起。将Table和Row替换为Object以解决错误并使其编译。

#1


9  

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:

这些类型,表和行不是Excel原生的变量类型。您可以通过以下两种方式之一解决此问题:

  1. 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 may like to fully qualify the objects like Dim oTable as Word.Table, oRow as Word.Row. This is called early-binding. '用户定义的类型未定义'错误
  2. 包括对Microsoft Word对象模型的引用。从工具|执行此操作引用,然后添加对MS Word的引用。虽然不是绝对必要,但您可能希望将像Dim oTable这样的对象完全限定为Word.Table,oRow as Word.Row。这称为早期绑定。
  3. 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.
  4. 或者,要使用后期绑定方法,必须将对象声明为通用对象类型:Dim oTable as Object,oRow as Object。使用此方法,您不需要添加对Word的引用,但您也失去了VBE中的intellisense帮助。

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

#2


-1  

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”引起。将Table和Row替换为Object以解决错误并使其编译。