【问题标题】:Exclude one sheet from being looped through by VBA Macro code排除一张表被 VBA 宏代码循环
【发布时间】:2021-07-22 21:08:15
【问题描述】:

我想在代码中循环排除 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! ", _
           "Hello Team, " & 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!", _
           "Hello Team, " & vbNewLine & vbNewLine & _
           "Check customer sheets: " & shtsRotations & vbLf & vbNewLine & _
           "In the attatched workbook, 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

【问题讨论】:

  • If sh.name &lt;&gt; "WB Mailling List" Then 作为循环内的第一行,另一个End If 作为循环内的最后一行。

标签: excel vba worksheet exclude-constraint


【解决方案1】:

您应该按名称或 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

【讨论】:

  • 不知道这是否是它被否决的原因,我不是那个人,但你真的应该避免Goto,因为一个简单的if...then 就可以完成这项工作。
  • 这不是避免 goto 的规则。这是我的解决方案。
  • 这可能是选民的意见认为 goto 不是在这里使用的正确方法。投票无论是向上还是向下都是感知质量的表达。请参阅:l3harrisgeospatial.com/Learn/Blogs/Blog-Details/ArtMID/10198/… 大多数编码人员的主要观点是,在可以使用简单 If Then 块的情况下使用 goto 是一种不好的做法。
  • 在微软的文档中甚至声明应该尽可能避免它:docs.microsoft.com/en-us/office/vba/language/reference/…
  • 好吧,没有goto))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-13
相关资源
最近更新 更多