热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将Excel信息移植到Word时出错编译错误:未定义用户定义的类型,

尝试运行此代码时出现编译错误:(此处仅显示了一部分,因为调试器

尝试运行此代码时出现编译错误:

(此处仅显示了一部分,因为调试器中仅突出显示了一位)

加粗的部分是给我带来麻烦的部分。

我活跃的参考是:


  • 应用程序的Visual Basic

  • microsoft Excel 16.0对象库

  • microsoft activeX数据对象6.1库

  • OLE自动化

  • microsoft Office 16.0对象库

我们正在Word和Excel 2016中运行。

任何帮助将不胜感激。


'This will delete any rows that have findings that took place prior to the date that was previously entered.
For CellNum = TotalRowNum To 1 Step -1
If Cells(CellNum,10) Next
CurrentRowNum = Cells(Rows.Count,2).End(xlUp).Row
'This sorts the findings remaining based on alphabetical order,to make the copy over to word easier as A is first... etc.
Range("A1:J" & CurrentRowNum).Sort key1:=Range("I1:I" & CurrentRowNum),order1:=xlAscending,Header:=xlNo
Dim lastrow As Long
lastrow = ThisWorkbook.Sheets("Pre-Transfer Table").Range("A1",ThisWorkbook.Sheets("Pre-Transfer Table").Range("A1").End(xlDown)).Rows.Count
'Designates the file that the data will be transferred to.
Dim stWordDocument As String
stWordDocument = InputBox("Please enter the name of the word file you have created for this report (Include '.doc').")
**Dim wdApp As Word.Application**
Dim wdDoc As Word.Document
Dim wdCell As Word.Cell
Dim i As Long
Dim j As Long
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim vaData As Variant
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Pre-Transfer Table")
'Creates a 2D array populated by all of the finding critera
ReDim vaData(1 To lastrow,1 To 8)
With wsSheet
vaData = .Range("A1:H" & lastrow)
End With
Set wdApp = New Word.Application
'Opens the word document by accessing the same folder the workbook is stored in
Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)
'Populates the tables with the corresponding criteria
k = 4
For i = 1 To lastrow
j = 0
For Each wdCell In wdDoc.Tables(k).Columns(2).Cells
j = j + 1
wdCell.Range.Text = vaData(i,j)
Next wdCell
k = k + 1
Next i
'Deletes the sheet used for sorting,as the code cannot run again unless this sheet is removed or the name of it is changed.
ThisWorkbook.Sheets("Pre-Transfer Table").Delete
With wdDoc
.Save
.Close
End With
wdApp.Quit
'Frees up memory by clearing these variables.
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "Your report has been generated.",vbInformation
End Sub

此部分给我错误:

Sheets("Other Findings").Select
**Range("Table2[ASSIGNED" & Chr(10) & "TO]").Select**
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("E" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Operation:=xlNone,SkipBlanks
:=False,Transpose:=False

在代码部分

'Creates the sheet where the resorting of data is done. All of the relevant columns are copied over to this table.
Sheets.Add
activeSheet.Name = "Pre-Transfer Table"
Sheets("Risk Ranked Findings").Select
Range("Table1[DetaILS]").Select
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues,SkipBlanks _
:=False,Transpose:=False
Sheets("Risk Ranked Findings").Select
Range("Table1[LOCATION]").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("B1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Risk Ranked Findings").Select
Application.CutCopyMode = False
Range("Table1[TYPE]").Select
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Risk Ranked Findings").Select
Range("Table1[RECOMMENDED" & Chr(10) & "actION]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("D1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Risk Ranked Findings").Select
Range("Table1[DUE DATE]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("E1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Application.CutCopyMode = True
Selection.NumberFormat = "m/d/yyyy"
Sheets("Risk Ranked Findings").Select
Range("Table1[ASSIGNED" & Chr(10) & "TO]").Select
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("F1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Risk Ranked Findings").Select
Range("Table1[RISK]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("I1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Risk Ranked Findings").Select
Range("Table1[FINDING" & Chr(10) & "DATE]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("J1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Application.CutCopyMode = False
Sheets("Risk Ranked Findings").Select
Range("S2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("M1").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Application.CutCopyMode = False
FindingRowNum = Range("M1")
'This area is where the "Other Findings" are sorted and transferred. There is probably some redundancy here so if anything is to be cleaned up it is
'most likely this.
Dim OtherRowNum As Long
Dim TotalRowNum As Long
Dim CurrentRowNum As Long
Sheets("Other Findings").Select
Range("O2").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Application.CutCopyMode = False
OtherRowNum = Range("M2")
TotalRowNum = OtherRowNum + FindingRowNum
Sheets("Other Findings").Select
Range("Table2[DetaILS]").Select
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("A" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Other Findings").Select
Range("Table2[LOCATION]").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("B" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Other Findings").Select
Application.CutCopyMode = False
Range("Table2[RECOMMENDED" & Chr(10) & "action]").Select
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("C" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Other Findings").Select
Range("Table2[DUE DATE]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("D" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Application.CutCopyMode = True
Selection.NumberFormat = "m/d/yyyy"
Sheets("Other Findings").Select
Range("Table2[ASSIGNED" & Chr(10) & "TO]").Select
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("E" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Other Findings").Select
Range("Table2[TYPE]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("I" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Sheets("Other Findings").Select
Range("Table2[FINDING" & Chr(10) & "DATE]").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Pre-Transfer Table").Select
Range("J" & FindingRowNum + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues,Transpose:=False
Application.CutCopyMode = False





推荐阅读
author-avatar
小胖菜鸟
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有