Excel VBA - 遍历电子表格并预先形成另一个循环

时间:2017-06-28 13:46:45

标签: excel vba excel-vba

我可能只是做错了什么。

我试图遍历每个工作表在工作簿中,在每个工作表上,我希望它能够预先形成另一个循环。问题是我没有进入下一个工作表,只能在活动工作表上工作。  任何帮助将不胜感激。

Sub Hours()     
    Dim ws As Worksheet
    Dim I As Integer

        For Each ws In ActiveWorkbook.Worksheets
            For I = 6 To 21

                k = I + 1

                If Cells(k, 7).Value = "" Then

                ElseIf Cells(I, 8).Value <> Cells(k, 7) Then
                     Cells(I, 8).Font.Color = vbRed
                     Cells(k, 7).Font.Color = vbRed
                End If
            Next I
    Next ws     
End Sub

4 个答案:

答案 0 :(得分:3)

尝试下面的代码,使用With ws语句获取所有对象,例如Cells符合您正在循环的当前Worksheet

<强> 代码

Sub Hours()

Dim ws As Worksheet
Dim i As Long, k As Long

For Each ws In ActiveWorkbook.Worksheets
    With ws
        For i = 6 To 21
            'k = i + 1 ' not needed, just change k to i + 1
            ' merge 2 Ifs to one using an And
            If .Cells(i + 1, 7).Value <> "" And .Cells(i, 8).Value <> .Cells(i + 1, 7) Then
                 .Cells(i, 8).Font.Color = vbRed
                 .Cells(i + 1, 7).Font.Color = vbRed
            End If
        Next i
    End With
Next ws

End Sub

答案 1 :(得分:0)

我用它循环遍历我的工作表:

     For i = 1 To ActiveWorkbook.Worksheets.Count
       ActiveWorkbook.Worksheets(i)
       'adapt your code into that
     Next i

它可能对你有用

答案 2 :(得分:0)

你必须告诉vba选择下一个工作表。现在它循环遍历你拥有的工作表的数量,但由于代码中没有任何东西可以激活下一个工作表,它只是在第一个工作表上循环。试试这个:

Sub Hours()
Dim ws As Worksheet
Dim I As Integer

    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        For I = 6 To 21

            k = I + 1

            If Cells(k, 7).Value = "" Then

            ElseIf Cells(I, 8).Value <> Cells(k, 7) Then
                 Cells(I, 8).Font.Color = vbRed
                 Cells(k, 7).Font.Color = vbRed
            End If
        Next I
Next ws

End Sub

为了给你一些额外的信息,我通常使用这段代码来处理工作表的循环:

sub example()
ws = ThisWorkbook.Worksheets.Count
for i = 1 to ws
'other code
next i
end sub

我喜欢这种方式,我更灵活,不必遍历所有工作表。我经常不想在第一个或最后一个工作表上执行流程。为了实现这一目标,我可以像这样调整for语句:

'to not loop through the last worksheet
for i = 1 to ws - 1

'to not loop through the first worksheet
for i = 2 to ws

我喜欢这种方式以获得更大的灵活性。当然,如果您想使用for each ws方法遍历每个工作表,则更简单,因为您不必定义变量。我只想扩展这个主题。

答案 3 :(得分:0)

考虑一下。

Sub Test1()
'UpdatebyExtendoffice20161222
      Dim x As Integer
      Dim ws As Worksheet

      ' Begin looping through the sheets
      For Each ws In ActiveWorkbook.Worksheets
      ws.Select
        ' Begin looping through the cells
        ' Set numrows = number of rows of data.
        NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
        ' Select cell a1.
        Range("A1").Select
        ' Establish "For" loop to loop "numrows" number of times.
        For x = 1 To NumRows
           ' Insert your code here.
           ' Selects cell down 1 row from active cell.
           ActiveCell.Offset(1, 0).Select

           ' After a blank cells is found, move to the next sheet
        Next

      Next ws

End Sub