Excel VBA 宏:未定义用户定义的类型 [英] Excel VBA Macro: User Defined Type Not Defined

查看:40
本文介绍了Excel VBA 宏:未定义用户定义的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试执行此宏时遇到上述错误.我对宏和编码很陌生,所以请原谅我的无知.

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,

这些类型,TableRow 不是 Excel 固有的变量类型.您可以通过以下两种方式之一解决此问题:

These types, Table and Row are not variable types native to Excel. You can resolve this in one of two ways:

  1. 包括对 Microsoft Word 对象模型的引用.从工具中执行此操作 |引用,然后添加对 MS Word 的引用.虽然不是绝对必要,但您可能喜欢完全限定对象,例如Dim oTable as Word.Table,oRow as Word.Row.这称为早期绑定.
  2. 或者,要使用后期绑定方法,您必须将对象声明为通用Object 类型:Dim oTable as Object,oRow as Object.使用此方法,您无需添加对 Word 的引用,但也会失去 VBE 中的智能感知帮助.
  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. 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.

我没有测试过您的代码,但我怀疑 ActiveDocument 在使用方法 #2 的 Excel 中不起作用,除非您将其范围正确地限定为 Word.Application 对象的实例.我在您提供的代码中的任何地方都没有看到.一个例子是:

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:

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

这篇关于Excel VBA 宏:未定义用户定义的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆