随机迭代期间发生错误91

这里有趣的问题。 这行代码通过多次迭代工作,直到达到它引发运行时错误91的点为止:“对象variables或块variables未设置”。 这是发生在一个function,旨在find一个交易号码。 整个程序是一天结束的电子邮件生成程序,发送附件到各种不同的对方。 错误发生在**行上。 对于额外的颜色,当尝试执行时,临时交易不是空的。 似乎没有任何多余的尾随或领先的空间。 提前致谢!

Function getPDFs(cFirm As Variant, iFirm As Variant, row_counter As Variant, reportsByFirm As Worksheet, trMaster As Worksheet, trSeparate As Variant, trName As Variant, reportDate As Variant) As String dealCol = 1 Dim locationArray() As String Dim DealArray() As String cDes = "_vs._NY" iDes = "_vs._IC" filePath = "X:\Office\Confirm Drop File\" dealNum = reportsByFirm.Cells(row_counter, dealCol) FileType = ".pdf" If InStr(1, dealNum, "-") > 0 Then DealArray() = Split(dealNum, "-") tempDeal = DealArray(LBound(DealArray)) Else tempDeal = dealNum End If 'Finds deal location in spread sheet for further detail to obtain file path **trLocation = trMaster.Columns(2).Find(What:=tempDeal).Address locationArray() = Split(trLocation, "$") trRow = locationArray(UBound(locationArray)) 'Formats client names for 20 characters and removes punctuation (".") in order to stay within convention of file naming cFirmFormatted = Trim(Left(cFirm, 20)) iFirmFormatted = Trim(Left(iFirm, 20)) 'Finds clearing method clMethod = trMaster.Cells(trRow, 6).Value Select Case clmethod Case "Clport" 'Prevents naming convention issues with punctuations in the name If InStr(1, cFirmFormatted, ".") > 0 Then cFirmFormatted = Replace(cFirmFormatted, ".", "") End If getPDFs = filePath & cFirmFormatted & "\" & reportDate & "_" & dealNum & "_" & cFirmFormatted & cDes & FileType Case "ICE" If InStr(1, iFirmFormatted, ".") > 0 Then iFirmFormatted = Replace(iFirmFormatted, ".", "") End If getPDFs = filePath & iFirmFormatted & "\" & reportDate & "_" & dealNum & "_" & iFirmFormatted & iDes & FileType End Select End Function 

你的代码假设总是findtrLocation ,如果没有find,那么你会收到一个错误,因为你没有范围返回.Address属性。

先尝试testing结果:

 Dim testLocation As Excel.Range Set testLocation = trMaster.Columns(2).Find(tempDeal) If Not testLocation Is Nothing Then trLocation = testLocation.Address '// Rest of code here... Else MsgBox "Cannot find """ & tempDeal & """!" Exit Function End If