Excel VBA应用程序或对象定义的错误 – 向表格写入variables值

我在下面的函数中得到了一个“应用程序或对象定义的错误”消息,这个函数正在把variables写回到表单中。 我已经尝试了几个表单vs工作表集合和范围与单元格对象的组合,但仍然收到相同的错误。 日志证实问题出现在调用.Range开头的行中,所有使用的自定义对象和方法都经过了unit testing。 我很担心这个问题来自于上面内置variables的错误使用,但是我正在努力寻找正确的语法。 援助将不胜感激!

Public Function LoadWorkflowIntoEditor(analysisWorkflowName As String) ' 'Load an existing workflow into the workflow editor in the list view ' WriteLogs ("LoadWorkflowIntoEditor Called") Dim tempAnalysisSet As Collection Set tempAnalysisSet = New Collection Dim tempKeyAction As AnalysisKeyAction Set tempKeyAction = New AnalysisKeyAction With Worksheets(WorkflowSheetName) For i = 1 To Master_FlowList.GetWorkflowByName(analysisWorkflowName).NumberOfKeyActions WriteLogs ("List Editor Row " & i & "Population Initiated") Set tempKeyAction = Master_FlowList.GetWorkflowByName(analysisWorkflowName).GetKeyActionByIndex(i) WriteLogs ("Key Action Pulled from Workflow List") Set tempAnalysisSet = Master_ModuleList.FindAnalysisSetByKeyActionName(tempKeyAction.AKeyActionName) WriteLogs ("Analysis Set pulled from Module List") .Range(.Cells(i + ListEditorStartRowNumber_WF, WorkflowColumnNumber_WF)).Value = CurrentWorkflowName .Range(.Cells(i + ListEditorStartRowNumber_WF, ModuleColumnNumber_WF)).Value = tempAnalysisSet.Item("Module") .Range(.Cells(i + ListEditorStartRowNumber_WF, SystemAreaColumnNumber_WF)).Value = tempAnalysisSet.Item("System Area") .Range(.Cells(i + ListEditorStartRowNumber_WF, KeyActionColumnNumber_WF)).Value = tempAnalysisSet.Item("Key Action") .Range(.Cells(i + ListEditorStartRowNumber_WF, KeyActionDescriptionColumnNumber_WF)).Value = tempKeyAction.AKeyActionDescription .Range(.Cells(i + ListEditorStartRowNumber_WF, KeyActionExpectedResultColumnNumber_WF)).Value = tempKeyAction.AKeyActionExpectedResult .Range(.Cells(i + ListEditorStartRowNumber_WF, NextKeyActionColumnNumber_WF)).Value = NextKeyActionListToString(tempKeyAction.GetNextKeyActionList, IPDelimiterCharacter) Next i End With WriteLogs ("Workflow loaded into list editor") End Function 

亚历克斯

这个:

  .Range(.Cells(i + ListEditorStartRowNumber_WF, WorkflowColumnNumber_WF)).Value = CurrentWorkflowName .Range(.Cells(i + ListEditorStartRowNumber_WF, ModuleColumnNumber_WF)).Value = tempAnalysisSet.Item("Module") .Range(.Cells(i + ListEditorStartRowNumber_WF, SystemAreaColumnNumber_WF)).Value = tempAnalysisSet.Item("System Area") .Range(.Cells(i + ListEditorStartRowNumber_WF, KeyActionColumnNumber_WF)).Value = tempAnalysisSet.Item("Key Action") .Range(.Cells(i + ListEditorStartRowNumber_WF, KeyActionDescriptionColumnNumber_WF)).Value = tempKeyAction.AKeyActionDescription .Range(.Cells(i + ListEditorStartRowNumber_WF, KeyActionExpectedResultColumnNumber_WF)).Value = tempKeyAction.AKeyActionExpectedResult .Range(.Cells(i + ListEditorStartRowNumber_WF, NextKeyActionColumnNumber_WF)).Value = NextKeyActionListToString(tempKeyAction.GetNextKeyActionList, IPDelimiterCharacter) 

使用With块可能更简单:

 With .Rows(i + ListEditorStartRowNumber_WF) .Cells(WorkflowColumnNumber_WF).Value = CurrentWorkflowName .Cells(ModuleColumnNumber_WF).Value = tempAnalysisSet.Item("Module") .Cells(SystemAreaColumnNumber_WF).Value = tempAnalysisSet.Item("System Area") .Cells(KeyActionColumnNumber_WF).Value = tempAnalysisSet.Item("Key Action") .Cells(KeyActionDescriptionColumnNumber_WF).Value = tempKeyAction.AKeyActionDescription .Cells(KeyActionExpectedResultColumnNumber_WF).Value = tempKeyAction.AKeyActionExpectedResult .Cells(NextKeyActionColumnNumber_WF).Value = NextKeyActionListToString( _ tempKeyAction.GetNextKeyActionList, IPDelimiterCharacter) End With 

…和.Range(.Cells(...))指出的删除.Range(.Cells(...))