Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA宏更改单元格颜色多页_Excel_Vba - Fatal编程技术网

Excel VBA宏更改单元格颜色多页

Excel VBA宏更改单元格颜色多页,excel,vba,Excel,Vba,我得到了一个宏,它检查数组中的所有表 并为启用值的范围M8上色。它工作,但在38个细胞后停止,并且不给任何细胞着色 有人能看一下吗?有什么问题吗 提前谢谢 Sub TestColour2() Dim st As Sheets Dim x As Integer Dim wsh As Worksheet Sheets(Array("T1", "E2", "S3", "M4", "S5", "F5")).Select For Each wsh In ActiveWindow.Selecte

我得到了一个宏,它检查数组中的所有表 并为启用值的范围M8上色。它工作,但在38个细胞后停止,并且不给任何细胞着色

有人能看一下吗?有什么问题吗

提前谢谢

Sub TestColour2()
Dim st As Sheets
Dim x As Integer
Dim wsh As Worksheet

Sheets(Array("T1", "E2", "S3", "M4", "S5", "F5")).Select

    For Each wsh In ActiveWindow.SelectedSheets
        Application.ScreenUpdating = False
       'st.Select
        NumRows = Range("M8", Range("M8").End(xlDown)).Rows.Count
        Range("M8").Select

        For x = 1 To NumRows
            ActiveCell.Interior.ColorIndex = 35
            ActiveCell.Offset(1, 0).Select
        Next x
    Next wsh
Application.ScreenUpdating = True
End Sub

下面是一个更高效、更可靠的宏,它将获得相同的结果,并且运行速度更快:

根据CLR的建议进行更新,立即为范围上色,而不是使用另一个循环

Sub TestColour2()
Dim sheetz As Variant, numrows As Integer, cel As Range, x As Integer
sheetz = Array("T1", "E2", "S3", "M4", "S5", "F5")

For x = 0 To UBound(sheetz)
    With Sheets(sheetz(x))
    numrows = .Range("M" & Rows.Count).End(xlUp).Row

        .Range("M8:M" & numrows).Interior.ColorIndex = 35
    End With
    Next

End Sub
但是,如果只需要对填充的单元格进行着色,我的原始答案仍然有效:

Sub TestColour2()
Dim sheetz As Variant, numrows As Integer, cel As Range, x As Integer
sheetz = Array("T1", "E2", "S3", "M4", "S5", "F5")

For x = 0 To UBound(sheetz)
    With Sheets(sheetz(x))
    numrows = .Range("M" & Rows.Count).End(xlUp).Row

        For Each cel In .Range("M8:M" & numrows)
            If cel.value = "" then cel.Interior.ColorIndex = 35
        Next cel
    End With
    Next

End Sub
或者,如果范围太大而无法循环通过,则可以使用过滤器进行此操作:

Sub TestColour2()
Dim sheetz As Variant, numrows As Integer, cel As Range, x As Integer
sheetz = Array("T1", "E2", "S3", "M4", "S5", "F5")

For x = 0 To UBound(sheetz)
    With Sheets(sheetz(x))
    numrows = .Range("H" & Rows.Count).End(xlUp).Row
    Debug.Print numrows & Sheets(sheetz(x)).Name


        .Range("H8:H" & numrows).AutoFilter field:="1", Criteria1:="<>"
        .Range("H8:H" & numrows).Rows.SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 35
        .AutoFilterMode = False
        End With
    Next

End Sub
Sub testcolor2()
尺寸表Z为变量,numrows为整数,cel为范围,x为整数
sheetz=阵列(“T1”、“E2”、“S3”、“M4”、“S5”、“F5”)
对于x=0至UBound(图Z)
带活页(活页Z(x))
numrows=.Range(“H”和Rows.Count).End(xlUp).Row
调试。打印numrows和Sheets(sheetz(x))。名称
.Range(“H8:H”和numrows)。自动筛选字段:=“1”,标准1:=“”
.Range(“H8:H”和numrows).Rows.SpecialCells(xlCellTypeVisible).Interior.ColorIndex=35
.AutoFilterMode=False
以
下一个
端接头

首先,最好使用条件格式。第二,尽可能多。第三,你的工作不可靠。(很可能是这里的问题)嗨。发生时NumRows的值是多少?36是停止时的数字M列中是否有空白单元格?测试后,代码的问题是,根据第一张图纸的行数,在选择所有图纸时,一次将一个单一颜色范围应用于所有图纸。因此,如果第一张纸有36行,那么所有的纸都将被着色到最多36行。请参阅下面的答案,以获得更可靠的方法。您不需要使用
cel
循环为每个单元格着色。您可以只对范围进行着色:
.range(“M8:M”&numrows).Interior.ColorIndex=35
@CLR很好,我完全没有想到这一点,因为我希望它尽可能接近原始代码。我会用你的建议更新我的答案。虽然这个问题提到了用一个值给所有单元格着色的问题,但这在最初的sub中没有完成。我认为这需要第二个循环,或者一个过滤器。感谢Plutian第一个代码工作正常,感谢所有其他人花时间回复。