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

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

我需要通过帐户编号,环境和产品名称对发票进行sorting。 使用types和相关成本填满了一个表格。

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

代码我已经很好的exLoop的第一次迭代。 我收到第一个帐号ID的正确发票。 当它移动到ExLoop的第二次迭代时,它会给我提供运行时错误91:对象variables或未设置块variables。 我尝试了这么多的方法,我不明白为什么它是第一次,而不是第二次。 这里是错误行的代码:

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 

这不是一个修复,但可以帮助您debugging代码,找出问题的发生地点。

在VBA中连接多个方法和属性时,这个错误是很常见的。 如果某个方法或属性没有返回有效的对象引用,那么您调用该对象的任何属性或方法都将失败。 如果在发生错误(而不是结束)时select“debugging”,则可以将鼠标hover在命令行的每个部分上,以查看哪一个未返回期望值。

有时,通过分解代码行并特别设置每个对象variables来更容易地find问题:

 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 

我怀疑最可能的问题将是查找返回无,因此行属性将失败。 但是,上述步骤可以帮助您确认是否属于这种情况。

一旦你知道问题发生在哪里,那么解决问题的原因就更容易了。