vba表格颜色范围格式

时间:2017-01-28 00:41:13

标签: excel vba excel-vba

大家好,我在电子表格中有40个标签,当我复制分组的图纸时,颜色会发生变化,

我已经编写了一个代码来更新受影响的工作表,但它不起作用,任何帮助将不胜感激。

Sub Color()

Application.ScreenUpdating = False
Application.Calculation = xlAutomatic
Application.DisplayAlerts = False

Dim ws As Worksheet
Dim ws1 As Worksheet
Dim r1 As Range
Dim r2 As Range
Dim rngs As Range

For Each ws In ActiveWorkbook.Sheets

    If ws.Name <> "A" And ws.Name <> "B" And ws.Name <> "C" And _
       ws.Name <> "D" And ws.Name <> "E" And ws.Name <> "F" Then    
        Set r1 = Range("B1:E3")
        Set r2 = Range("N1:P3")

        Set rngs = Union(r1, r2)

        With rngs.Interior
           .PatternColorIndex = xlAutomatic
           .Color = 49407
           .TintAndShade = 0
           .PatternTintAndShade = 0
        End With
    End If
Next ws    

Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
Application.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:1)

尝试通过更改以下内容来确定使用工作表限定范围:

Set r1 = Range("B1:E3")
Set r2 = Range("N1:P3")

为:

Set r1 = ws.Range("B1:E3")
Set r2 = ws.Range("N1:P3")