我对VBA很新。我正在尝试开发一个宏来读取Excel文件并在Project文件中填充数据。 Excel文件大约有800行。我正在调试代码。在填充Project文件时,在For循环中,多次执行循环后,我得到"运行时错误' 1101':参数值无效。" 能否请您帮助我了解问题?请在下面找到代码。 问候 科亚
Sub upload_excel_to_mpp()
'Declaration
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim vaWorkorder As Variant
Dim vaTaskname As Variant
Dim vaHours As Variant
Dim vaArea As Variant
Dim vaSkill As Variant
Dim vaStatus As Variant
Dim lnStart As Long
Dim lnLastrow As Long
Dim lnCounter As Long
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets(1)
'Populate the variables with values from Worksheet
With wsSheet
lnStart = .Range("D65536").End(xlUp).Row
vaWorkorder = .Range("A2:A" & lnStart).Value
vaTaskname = .Range("E2:E" & lnStart).Value
vaHours = .Range("F2:F" & lnStart).Value
vaArea = .Range("G2:G" & lnStart).Value
vaSkill = .Range("H2:H" & lnStart).Value
End With
'Instantiate and open MS Project and the project
'Need to add MS project libraries by going to tools->references
Dim prApp As MSProject.Application
Dim prProject As MSProject.Project
Set prApp = New MSProject.Application
prApp.FileOpen "C:\Users\manub\Desktop\work\Quantas\Qantaslink template.mpp"
Set prProject = prApp.ActiveProject
'Add task and other wanted information to the project.
With prProject
For lnCounter = 1 To UBound(vaTaskname)
.Tasks.Add vaTaskname(lnCounter, 1)
With .Tasks(vaTaskname(lnCounter, 1))
.Text2 = vaWorkorder(lnCounter, 1)
.Work = vaHours(lnCounter, 1)
.Text8 = vaSkill(lnCounter, 1)
.Text6 = vaArea(lnCounter, 1)
End With
Next lnCounter
End With
'Save the project and close MS Project.
With prApp
.FileSave
.Quit
End With
MsgBox "Done!", vbInformation
'Release objects from memory.
Set prProject = Nothing
Set prApp = Nothing
End Sub
答案 0 :(得分:0)
在该行之前添加一行代码:If InCounter = 497 Then Debug.Print vaHours(497, 1)
。看一下立即窗口中的值。应该有一些问题,这使得它与列中的所有其他内容不同,可能是Text而不是数字。当然,您也可以在工作表中找到该值。如果错误与数字497有关,请将其移动到工作表中的另一个位置。请记住,您可以通过将光标悬停或在代码执行暂停时将它们打印到立即窗口来检查变量值。
在第With .Tasks(vaTaskname(lnCounter, 1))
行添加断点。您可以通过单击代码左侧的灰色栏,在指示的行旁边执行此操作。然后运行代码。它将在断点处停止。按F8键前进一行。按F5将运行其余代码,但由于这意味着重复循环,它将再次停在相同的断点处。停止时,您可以在立即窗口中询问任何值,或通过将鼠标悬停在它们上来读取变量值。了解发生了什么。
请不要说你很遗憾你在休假。这让我悲伤。 : - )