如何使用For Each循环只需几张

时间:2015-06-22 11:20:29

标签: excel vba

我正在尝试使用此代码在几张表之间进行迭代,但它不起作用

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

1 个答案:

答案 0 :(得分:0)

从我所看到的情况来看,你是以一种奇怪的方式设置你的目标表单列表,这些表格不太合适......这样的事情怎么样?

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