簡體   English   中英

如何使用僅幾張紙的For Each循環

[英]How to Use a For Each loop with just few sheets

我正在嘗試使用此代碼在幾張紙之間進行迭代,但它不起作用

Sub ContaNoPrazo()
   Dim conta As Integer
   Dim Array1 As String
   Array1 = ThisWorkbook.Sheets(Array   ("Isabel", "Thiago", "Victor", "Natacha", "Stefano"))
   conta = 0
   Sheets("Isabel").Activate
   For Each planilha In Array1
       Range("I2").Select
       Range(Selection, Selection.End(xlDown)).Select
       For Each MyCell In Selection
           If MyCell.Value <= 5 And MyCell.Value >= 0 Then
              conta = conta + 1
           End If
       Next MyCell
   Next planilha
   Sheets("Analise").Activate
   Cells(2, 1).Value = conta
End Sub

從我所看到的,您正在以一種奇怪的方式來建立目標工作表列表,這種方法不太可行。

Sub ContaNoPrazo_v2()

    Dim conta As Integer
    'Dim Array1 As String
    Dim sht As Worksheet

    'Array1 = ThisWorkbook.Sheets(Array("Isabel", "Thiago", "Victor", "Natacha", "Stefano"))
    conta = 0
    Sheets("Isabel").Activate

    For Each sht In ThisWorkbook.Worksheets

        'only enter this loop if in your target names
        If sht.Name = "Isabel" Or sht.Name = "Thiago" Or sht.Name = "Victor" _
            Or sht.Name = "Natacha" Or sht.Name = "Stefano" Then

            sht.Activate
            Range("I2").Select
            Range(Selection, Selection.End(xlDown)).Select

            For Each MyCell In Selection

                If MyCell.Value <= 5 And MyCell.Value >= 0 Then
                    conta = conta + 1
                End If

            Next MyCell

        End If

    Next sht

    Sheets("Analise").Activate
    Cells(2, 1).Value = conta

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM