我当前正在使用一个遍历两个数据范围的for循环-如果它看到第一行中的某个单元格具有单词“ Rejected”,而第二行为空,则意味着该单元格将被挂起,然后在退出该小组之前产生一封电子邮件。
再次启动循环后,它的意思是找到满足这些参数的下一行。但是,它只会再次进入第一个。
我试图通过确保它用'pending'填充该单元格来否定此点-这意味着它不再是空的,但它仍然只到达那里。
代码如下:
Dim i As Long 'Defining i, which will be used to loop through the ranges.
For i = 6 To 50000 'Setting the ranges -
If Range("M" & i) = "Rejected" And Range("N" & i) = "" Then 'Searches through the ranges first job that the partner has rejected - and then checks as to whether the partner has issued their assessment.
Range("O" & i) = "'Pending" 'Changes the first one that has been to 'Pending' so it won't be picked up the next time the code is run.
GoTo Email
End If
Next
我不太确定该怎么做。一旦发送完电子邮件,我就需要停止该sub,否则它可能会循环并一次创建数百个,这不仅会使系统超负荷,而且会淹没我的收件箱。
P,不确定为什么我的代码格式怪异-抱歉
答案 0 :(得分:-1)
问题是您正在使用GoTo
并执行了应做的操作,请转到...您的邮件标签。一旦到达那里,它将继续使代码下降,而永远不会上升,就像跳过循环一样。相反,您可以将电子邮件代码放入另一个过程中,并在必要时调用它,这样循环将继续:
Option Explicit
Sub Test()
Dim C As Range 'this will loop through the range
Dim LastRow As Long 'this will find the last row
With ThisWorkbook.Sheets("MySheetName") 'change MySheetName
LastRow = .Cells(.Rows.Count, "M").End(xlUp).Row 'last row with data on column M
For Each C In .Range("M6:M" & LastRow) 'loop through M6 to M & last row
If C = "Rejected" And .Cells(C.Row, "N") = vbNullString And Not .Cells(C.Row, "O") = "'Pending" Then 'vbnullstring has better performance than ""
.Cells(C.Row, "O") = "'Pending"
Mail C.Row
End If
Next C
End With
End Sub
Sub Mail(MailRow As Long)
'Code doing your mail
End Sub