2015-12-21 118 views
0

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

谢谢!

'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 
+3

“row_counter”,“firmcol”何时失效,值是多少? –

+0

@TimWilliams公司列固定为5,row_counter为28. LRowReportsByFirm在这种情况下是34 – StormsEdge

+2

错误的文本是什么,并且这些单元格中的任何一个是否包含错误值? –

回答

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