在所有工作表上格式化特定的单元格集

时间:2017-02-06 14:42:08

标签: excel vba excel-vba

下面的代码并没有给出任何错误,但它并没有完全按照我的意愿行事,而且我不确定我做错了什么。

代码的目的是根据每张纸上的标准将公式添加到所有纸张中的特定单元格中。这部分公式工作得非常好(如果它可以缩短,那么这将是一个奖励)。没有正常工作的部分是代码的格式化部分。我希望定义的格式在所有工作表上发生,但它只发生在第一个"摘要"片。

正如我之前提到的,没有错误消息,它运行良好,但只更改摘要表中的单元格格式而不是所有工作表。

非常感谢任何建议:)

Sub Summary()

    Dim wb1 As Workbook
    Dim Sht As Worksheet
    Dim Rng, Rng2 As Range
    Dim cell As Range
    Dim ws As Worksheet

    Set wb1 = ThisWorkbook
    Set Sht = wb1.Worksheets("Summary")
    Set Rng = Sht.Range("A6:A" & Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row)

    For Each cell In Rng
       Set ws = wb1.Sheets(cell.Text)
       Select Case ws.Range("A4").Value
                Case "Standard Kitchen Template"
                ws.Range("G10").Formula = "=Sum(e2167:e2182, e2179:e2885)"
                ws.Range("H10").Formula = "=Sum(e49:e54, e291:e296)"
                ws.Range("I10").Formula = "=Sum(e125:e139)"
                ws.Range("J10").Formula = "=Sum(e213:e286, e299:e302)"
                ws.Range("K10").Formula = "=Sum(e168:e208)"
                ws.Range("L10").Formula = "=Sum(e156:e162)"
                ws.Range("O10").Formula = "=Sum(e142:e148)"
                ws.Range("Q10").Formula = "=Sum(e14:e48, e56:e78)"

                Case "Standard Bathroom Template"
                ws.Range("G10").Formula = "=Sum(e334:e339, e347:e1050)"
                ws.Range("H10").Formula = "=Sum(e185:e317)"
                ws.Range("I10").Formula = "=Sum(e79:e97)"
                ws.Range("J10").Formula = "=Sum(e68:e70, e323:e326)"
                ws.Range("K10").Formula = "=Sum(e134:e178)"
                ws.Range("L10").Formula = "=Sum(e115:e132)"
                ws.Range("O10").Formula = "=Sum(e99:e107)"
                ws.Range("Q10").Formula = "=Sum(e29:e33, e41:e50)"

End Select
Next cell   '<------The code below this line is the one not working properly

For Each Sht In ThisWorkbook.Sheets
 With Sht
        Range("G10").Select
        With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 12611584
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("H10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("I10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 49407
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("J10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("K10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("L10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 10498160
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("O10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent3
        .TintAndShade = -0.249977111117893
        .PatternTintAndShade = 0
    End With
    Range("Q10").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With
    End With

     Next

    End Sub

1 个答案:

答案 0 :(得分:1)

For Each Sht In ThisWorkbook.Sheets循环中,您没有使用Sht对象。您指的是ActiveSheet范围内的范围,因为.Select方法的前缀不是.,而是将范围与Sht对象相关联。

例如,在您的With Sht后,您需要下一行.Range("G10").Select而不是Range("G10").Select - 请注意开头的.

您之前已在代码中使用Sht变量来定义wb1.Worksheets("Summary"),因此最好在循环中使用其他变量以避免混淆。

但是,始终建议不要使用Select,因为这会降低代码速度。试试这个:

For Each Sht In ThisWorkbook.Sheets
    With Sht
        With .Range("G10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 12611584
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .Range("H10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .Range("I10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 49407
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .Range("J10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .Range("K10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 5296274
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .Range("L10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 10498160
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .Range("O10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = -0.249977111117893
            .PatternTintAndShade = 0
        End With
        With .Range("Q10").Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.149998474074526
            .PatternTintAndShade = 0
        End With
    End With

Next Sht