第二次迭代时的VBA运行时错误91

时间:2015-06-26 18:11:02

标签: excel vba excel-vba runtime-error

我写了一些代码,允许我通过从大型数据电子表格中提取数据并使用这些信息填充空白发票来创建发票。

我需要按帐户ID号,环境和产品名称对发票进行排序。使用类型和相关成本填满了表格。

我有一个工作程序来为每个环境创建发票,但我现在也需要根据帐户ID进行分类。

我在exLoop的第一次迭代中运行的代码很好。我得到第一个帐户ID号的正确发票。当它移动到ExLoop的第二次迭代时,它给出了运行时错误91:对象变量或未设置块变量。我已经尝试了很多这样的方法而且我不理解为什么它第一次工作而不是第二次工作。这是带错误行的代码:

Sub CreateInvoices()

Dim bookName, sheetName, accountId, accountName As String
Dim usageType, totalCost, productCode, productName, environment As String
Dim myDate, path, myFileName As String
Dim invoiceRow, tempRow As Long
Dim mainLoop, outerLoop, exLoop, row As Long
Dim firstAccountName, lastAccountName, firstEnviroName, lastEnviroName, firstProductName, lastProductName As Long
Dim accountRng, enviroRng, prodRng As Range
Dim accountList, enviroList, prodList As Object
Dim accountNameCount, enviroNameCount, prodNameCount, lastRow As Integer

'initialize data workbook and worksheet names
bookName = ActiveWorkbook.Name
sheetName = ActiveSheet.Name
'initialize first row of environment and product names
firstAccountName = 2
firstEnviroName = 2
firstProductName = 2

'find last row of data
lastRow = Sheets(sheetName).Range("A" & Rows.Count).End(xlUp).row

'sort by usage type, then product name, then environment, then accountId
Range("P1").Columns(16).Sort Key1:=Range("P1"), Order1:=xlAscending, Header:=xlYes
Range("N1").Columns(14).Sort Key1:=Range("N1"), Order1:=xlAscending, Header:=xlYes
Range("AE1:AE" & lastRow).Sort Key1:=Range("AE1"), Order1:=xlAscending, Header:=xlYes
Range("C1").Columns(3).Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlYes

'determine the number of unique linked accounts
Set accountList = CreateObject("Scripting.Dictionary")
For Each accountRng In Range("C2:C" & lastRow)
    If Not accountList.Exists(accountRng.Value) Then accountList.Add accountRng.Value, Nothing
Next
accountNameCount = accountList.Count

For exLoop = 0 To accountNameCount - 2

    'Activate data sheet
    Workbooks(bookName).Sheets(sheetName).Activate
    'Find last row with current account name
    lastAccountName = Sheets(sheetName).Range("C2:C" & lastRow).Find(What:=accountList.keys()(exLoop), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row 'THIS IS WHERE THE ERROR HAPPENS ON THE SECOND ITERATION OF EXLOOP

    'determine the number of unique environments
    Set enviroList = CreateObject("Scripting.Dictionary")
    For Each enviroRng In Range("AE" & firstAccountName & ":AE" & lastAccountName)
        If Not enviroList.Exists(enviroRng.Value) Then enviroList.Add enviroRng.Value, Nothing
    Next
    enviroNameCount = enviroList.Count

    'outer loop controls us making a new groups of invoices for each environment
    For outerLoop = 0 To enviroNameCount - 1

        'Activate data sheet
        Workbooks(bookName).Sheets(sheetName).Activate
        'Find last row with current environment
        lastEnviroName = Sheets(sheetName).Range("AE" & firstAccountName & ":AE" & lastAccountName).Find(What:=enviroList.keys()(outerLoop), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlPrevious).row

        'determine the number of unique product names
        Set prodList = CreateObject("Scripting.Dictionary")
        For Each prodRng In Range("N" & firstEnviroName & ":N" & lastEnviroName)
            If Not prodList.Exists(prodRng.Value) Then prodList.Add prodRng.Value, Nothing
        Next
        prodNameCount = prodList.Count

        'main loop controls us making a new invoice for each product name
        For mainLoop = 0 To prodNameCount - 1

'MORE CODE HERE TO EXTRACT THE DATA, POPULATE THE TEMPLATE AND SAVE THE FILE

        'update first product name row to the next product name
        firstProductName = lastProductName + 1

        Next mainLoop

    'update first environment row to the next environment
    firstEnviroName = lastEnviroName + 1

    Next outerLoop

'update first account row to next account
firstAccountName = lastAccountName + 1

Next exLoop

End Sub

1 个答案:

答案 0 :(得分:4)

这不是一个修复,但可以帮助您调试代码以找出问题发生的位置。

当在VBA中将许多方法和属性链接在一起时,此错误非常常见。如果其中一个方法或属性未返回有效的对象引用,则在该对象上调用的任何属性或方法都将失败。如果在发生错误时选择Debug(而不是End),则可以将鼠标悬停在命令行的每个部分上,以查看哪个部分未返回预期值。

有时通过分解代码行并专门设置每个对象变量来更容易找到问题:

Dim sheet As Worksheet
Dim r As Range
Dim rFound As Range
Dim search As Variant

Set sheet = Sheets(sheetName)
Set r = sheet.Range("AE" & firstAccountName & ":AE" & lastAccountName)
search = enviroList.keys()(outerLoop)
Debug.Print search
Set rFound = r.Find(What:=search, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlPrevious)
lastEnviroName = rFound.Row

我怀疑最可能的问题是Find返回Nothing,因此Row属性会失败。但是,上述步骤可以帮助您确认是否是这种情况。

一旦你知道问题发生在哪里,就可以更容易找出原因。