Excel VBA Application or Object-Defined Error - Writing Variable Values to Sheets

时间:2015-07-13 21:05:31

标签: excel vba excel-vba

I am getting an 'Application or Object Defined Error' message in the below function, which is writing variables back to a sheet. I have tried several combinations of Sheets vs Worksheets collections and Range vs Cells objects, but still recieve the same error. The logs confirm that the the issue is coming in the lines beginning with the call to .Range, and all custom objects and methods being utilized have been unit tested. I'm quite confidant that the issue is coming from an incorrect use of the above built-in variables, but I'm struggling to find the correct syntax. Assistance would be much appreciated!

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

Alex

1 个答案:

答案 0 :(得分:1)

此:

        .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

...并删除@Sorceri指出的.Range(.Cells(...))