问题描述
我想从代码中的循环中排除 1 个工作表,我该如何添加/执行该操作?我想通过代码(“WB 邮件列表”)运行我的邮件列表。我尝试了在不同论坛中找到的几个不同的建议,但似乎没有一个真正适合我。有没有一种简单易行的方法来排除一张纸被循环穿过?我是 vba 新手,所以真的可以使用帮助!谢谢!
Option Explicit
Sub Main_AllWorksheets()
Dim sh As Worksheet,i As Long,shtsRotations As String
Dim shtsFunctions As String,shtsOK As String
Dim shtsManufacture As String
For Each sh In ActiveWorkbook.Worksheets
If Application.CountIf(sh.Range("O3:O70"),"<1") > 0 Then
shtsRotations = shtsRotations & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Rotations)"
End If
If Application.CountIf(sh.Range("P3:P70"),"<1") > 0 Then
shtsFunctions = shtsFunctions & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Functions)"
End If
If Application.CountIf(sh.Range("Q3:Q70"),"<1") > 0 Then
shtsManufacture = shtsManufacture & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Manufacturing Date)"
End If
Next sh
Dim myDataRng As Range
Set myDataRng = Worksheets("WB Mailing List").Range("A1:Z100" & Cells(Rows.Count,"S").End(xlUp).Row)
Dim cell As Range
Dim iCnt As Integer
Dim sMail_ids As String
For Each cell In myDataRng
If Trim(sMail_ids) = "" Then
sMail_ids = cell.Offset(1,0).Value
Else
sMail_ids = sMail_ids & vbCrLf & ";" & cell.Offset(1,0).Value
End If
Next cell
Set myDataRng = nothing ' Clear the range.
If Len(shtsRotations) > 0 Then
SendReminderMail sMail_ids,"Equipment rotations are due!",_
"Hello Team," & vbNewLine & vbNewLine & _
"Check customer sheets: " & shtsRotations & vbLf & vbNewLine & _
"In the attatched workbook,you can see what equipment needs to be rotated by the red dates,indicating their last rotation."
End If
If Len(shtsFunctions) > 0 Then
SendReminderMail sMail_ids,"Equipment functions are due! "," & vbNewLine & vbNewLine & _
"Check customer sheets: " & shtsFunctions & vbLf & vbNewLine & _
"In the attatched workbook,you can see what equipment needs to be functioned by the red dates,indicating their last function."
End If
If Len(shtsManufacture) > 0 Then
SendReminderMail sMail_ids,"Manufacturing date has surpassed 3 years!",you can see what equipment has reached it's 3 years past manufacturing."
End If
If Len(shtsOK) > 0 Then
MsgBox "These sheets are OK: " & vbLf & shtsOK,vbinformation
End If
End Sub
解决方法
您应该按名称或 ID 捕捉工作表以跳过它。
-
在 For ... 之后添加这一行
如果不是 sh.Name="WB Mailing List" Then ... End If
请将您的 For 语句更改为:
For Each sh In ActiveWorkbook.Worksheets
If Not sh.Name="WB Mailing List" Then
If Application.CountIf(sh.Range("O3:O70"),"<1") > 0 Then
shtsRotations = shtsRotations & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Rotations)"
End If
If Application.CountIf(sh.Range("P3:P70"),"<1") > 0 Then
shtsFunctions = shtsFunctions & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Functions)"
End If
If Application.CountIf(sh.Range("Q3:Q70"),"<1") > 0 Then
shtsManufacture = shtsManufacture & vbLf & sh.Name
Else
shtsOK = shtsOK & vbLf & sh.Name & " (Manufacturing Date)"
End If
End if
Next sh