多次执行后If语句出错

时间:2015-12-21 21:08:39

标签: excel vba excel-vba outlook

我执行多次后在一行代码上收到错误。它在达到相同的执行级别时发生(宏试图完成的目标中的相同位置)。整个计划旨在生成多个电子邮件,并将其发送给具有不同电子邮件附件的不同公司。它一直工作,直到它达到大约2 / 3rds的方式,我得到1004错误。有任何想法吗? **是错误的位置。下面的代码是一个更大的例程的小片段。

谢谢!

'sets row_counter = row_num to avoid iteration over unecessary rows in ReportsbyFirm and resets continue to True for PDF attachment phase
    continue = True
    row_counter = row_num

    With outMail
        .To = firmEmail
        .Subject = reportDate 
        .body = body

        Do While continue = True

           ** If reportsByFirm.Cells(row_counter, firmcol) = cFirm Or reportsByFirm.Cells(row_counter, firmcol) = iFirm Then
                pdfLocation = getPDFs(cFirm, iFirm, row_counter, reportsByFirm, trMaster, trSeparate, trName, reportDate)
                .Attachments.Add (pdfLocation)
                row_counter = row_counter + 1

            ElseIf row_counter <> lRowReportsByFirm Then
                row_counter = row_counter + 1

            Else
                continue = False
            End If
        Loop
        .Display
    End With

1 个答案:

答案 0 :(得分:0)

循环计数器发生错误。逻辑不满足总是最终满足于false,这将导致row_counter结束于104K +值。由此修复

    Do While continue = True

            If reportsByFirm.Cells(row_counter, firmcol) = cFirm Or reportsByFirm.Cells(row_counter, firmcol) = iFirm Then
                pdfLocation = getPDFs(cFirm, iFirm, row_counter, reportsByFirm, trMaster, trSeparate, trName, reportDate)
                .Attachments.Add (pdfLocation)
                row_counter = row_counter + 1

            ElseIf row_counter < lRowReportsByFirm Then
                row_counter = row_counter + 1

            ElseIf row_counter >= lRowReportsByFirm Then
                continue = False
            End If
        Loop
        .Display
    End With