在迭代期间随机发生错误91

时间:2015-12-23 13:40:16

标签: excel vba excel-vba

这里有趣的问题。这行代码通过多次迭代工作,直到它到达它向我抛出运行时错误91的点:"对象变量或没有设置块变量"。这发生在旨在查找交易号的功能中。整个程序是一个结束日期的电子邮件生成程序,它将附件发送给各种不同的对方。错误发生在**行上。对于其他颜色,尝试执行时临时处理不为空。似乎没有任何无关的尾随或前导空格。提前谢谢!

    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

1 个答案:

答案 0 :(得分:3)

您的代码假设始终找到trLocation,如果找不到,那么您将收到错误,因为您没有范围可以返回.Address属性。

首先尝试测试结果:

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