[英]Excel VBA Macro: User Defined Type Not Defined
嘗試執行此宏時出現上述錯誤。 我對宏和一般編碼很陌生,所以請原諒我的無知。
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
您的錯誤是由以下原因引起的:
Dim oTable As Table, oRow As Row,
這些類型Table
和Row
不是 Excel 固有的變量類型。 您可以通過以下兩種方式之一解決此問題:
Dim oTable as Word.Table, oRow as Word.Row
等對象完全限定Dim oTable as Word.Table, oRow as Word.Row
。 這稱為早期綁定。Object
類型: Dim oTable as Object, oRow as Object
。 使用此方法,您無需添加對 Word 的引用,但也會失去 VBE 中的智能感知幫助。 我沒有測試過你的代碼,但我懷疑ActiveDocument
不能在 Excel 中使用方法 #2,除非你將它正確地限定到 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
我參加聚會遲到了。 嘗試替換如下,我的工作完美 - “DOMDocument”到“MSXML2.DOMDocument60”“XMLHTTP”到“MSXML2.XMLHTTP60”
Sub DeleteEmptyRows()
Worksheets("YourSheetName").Activate
On Error Resume Next
Columns("A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
以下代碼將刪除工作表 (YourSheetName) 上 A 列內容為空的所有行。
編輯:未定義的用戶定義類型是由“oTable As Table”和“oRow As Row”引起的。 用 Object 替換 Table 和 Row 以解決錯誤並使其編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.