代码不循环通过工作表

时间:2015-10-02 10:12:20

标签: excel vba excel-vba

我的工作簿有几张名为Detail1,Detail2,Detail3等的工作表。我想根据细胞B15的含量为片材着色。但是,代码只是着色表Detail1而不是在其他工作表中循环。我做错了什么?

    Sub Color_by_cell_value()
Dim X As String
Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name Like "Detail*" Then
            X = Range("B15").Value
            Select Case X
            Case Is = "no"
                Range("A1:AZ100").Select
                With Selection.Interior
                    .ThemeColor = xlThemeColorAccent3
                    .TintAndShade = 0.799981688894314
                End With
                Range("B15").Select
                With Selection.Interior
                    .Color = 5296274
                End With
            Case Is = "yes"
                Range("A1:AZ100").Select
                With Selection.Interior
                    .ThemeColor = xlThemeColorAccent2
                    .TintAndShade = 0.599993896298105
                End With
                Range("B15").Select
                With Selection.Interior
                    .Color = 255
                End With
            Case Is = "yes/no"
                Range("A1:AZ100").Select
                With Selection.Interior
                    .ThemeColor = xlThemeColorAccent6
                    .TintAndShade = 0.799981688894314
                End With
                Range("B15").Select
                With Selection.Interior
                    .Color = 65535
                End With
            End Select
        End If
    Next ws
End Sub

2 个答案:

答案 0 :(得分:1)

有很多方法可以编辑此代码并提高效率,但要回答您的问题。在行之后需要ws.select。如ws.Name Like "Detail*" Then

  For Each ws In Worksheets
    If ws.Name Like "Detail*" Then
        ws.Select
        X = Range("B15").Value
        Select Case X

答案 1 :(得分:0)

您还可以使用ws.Activate

Sub Color_by_cell_value()
Dim X As String
Dim ws As Worksheet

Application.ScreenUpdating = False

For Each ws In Worksheets

    If ws.Name Like "Detail*" Then

        ws.Activate
        X = Range("B15").Value

        Select Case X
        Case Is = "no"
            Range("A1:AZ100").Select
            With Selection.Interior
                .ThemeColor = xlThemeColorAccent3
                .TintAndShade = 0.799981688894314
            End With
            Range("B15").Select
            With Selection.Interior
                .Color = 5296274
            End With
        Case Is = "yes"
            Range("A1:AZ100").Select
            With Selection.Interior
                .ThemeColor = xlThemeColorAccent2
                .TintAndShade = 0.599993896298105
            End With
            Range("B15").Select
            With Selection.Interior
                .Color = 255
            End With
        Case Is = "yes/no"
            Range("A1:AZ100").Select
            With Selection.Interior
                .ThemeColor = xlThemeColorAccent6
                .TintAndShade = 0.799981688894314
            End With
            Range("B15").Select
            With Selection.Interior
                .Color = 65535
            End With
        End Select
    End If
Next ws

Application.ScreenUpdating = True

End Sub

注意我还将Application.ScreenUpdating设置为False作为一个很好的习惯。