我想創建一個循環,會去翻客戶的名單,如果有該客戶的一份報告,電子郵件客戶的報告。上的錯誤轉到環路
我需要的是一個On Error語句,將允許客戶在不報告被跳過,並允許腳本繼續到下一個顧客的權利,直到客戶列表的末尾。
On Error語句我現在畢竟客戶一直循環通過卡住,並在On Error語句繼續循環。
任何幫助將不勝感激!
sub test()
a = 2
Check:
Do Until UniqueBuyer.Range("A" & a).Value = ""
On Error GoTo ErrHandler:
Sheets(UniqueBuyer.Range("A" & a).Value).Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FolderLocation & FolderName & "\" & _
UniqueBuyer.Range("A" & a).Value & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=Flase, OpenAfterPublish:=False
PDFFile = FolderLocation & FolderName & "\" & _
UniqueBuyer.Range("A" & a).Value & ".pdf"
Set OutLookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutLookApp.createItem(0)
CombinedEmail = ""
'Clear variable - LK
On Error Resume Next
'Display email and specify To, Subject, etc
With OutlookMail
.Display
c = 4
Do Until UniqueBuyer.Cells(a, c).Value = ""
AdditionalEmail = UniqueBuyer.Cells(a, c)
CombinedEmail = CombinedEmail & ";" & AdditionalEmail
.to = CombinedEmail
c = c + 1
Loop
.cc = ""
.BCC = ""
.Subject = "Weekly Wooltrade Summary " & Left(Master.Range("X2"), 3)
.Body = ""
.Attachments.Add PDFFile
'.Send
End With
On Error GoTo 0
a = a + 1
Loop
Exit Sub
ErrHandler:
a = a + 1
GoTo Check
End Sub
不能使用'GoTo'退出錯誤處理程序。使用'恢復檢查'而不是'GoTo Check'。 「Check」標籤可能應該在循環內部,而不是在外部。也許就在'a = a + 1'行之前。 –
謝謝@VincentG! 會投入檢查標籤剛過A = A + 1線和環行線之前會更好? 使得不從= 2跳到= 4例如? – Harry
之前A = A + 1把,並移除處理程序的同一行是最好的選擇,恕我直言,但我還沒有到你的代碼的細節。 –