使用For Each遍历我的所有工作表VBA

时间:2017-06-08 12:44:42

标签: excel vba excel-vba

为什么这个宏不会改变我所有工作表的颜色?
它仅适用于我的活动工作簿的第一个工作表 我希望它能够浏览我工作簿的所有工作表。 感谢名单

Option Explicit

Private Sub CheckBox13_Click()
    Dim I As Long, j As Long
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        If CheckBox13.Value = True Then
            For I = 1 To 700
                For j = 1 To 10
                    If Cells(I, j).Interior.Color = RGB(252, 252, 250) Then
                        Cells(I, j).Interior.Color = RGB(217, 217, 217)
                    End If
                Next j
            Next I
        End If

        If CheckBox13.Value = False Then
            For I = 1 To 700
                For j = 1 To 10
                    If Cells(I, j).Interior.Color = RGB(217, 217, 217) Then
                        Cells(I, j).Interior.Color = RGB(252, 252, 250)
                    End If
                Next j
            Next I
        End If
    Next
End Sub

1 个答案:

答案 0 :(得分:3)

当您使用引用活动工作表的Cells(I, j)时。您需要在参考中使用ws对象,如下所示:

ws.Cells(I, j)
相关问题