Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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 使用宏更改当前行的颜色_Excel_Vba - Fatal编程技术网

Excel 使用宏更改当前行的颜色

Excel 使用宏更改当前行的颜色,excel,vba,Excel,Vba,我正在尝试使用excel中的表单控件按钮来更改整行的颜色,并能够在以下行上复制该按钮,但仅使按钮操作影响与所述按钮对应的行。当前宏是: Sub RED() Rows("3:3").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 10066431 .TintAndShade = 0 .PatternT

我正在尝试使用excel中的表单控件按钮来更改整行的颜色,并能够在以下行上复制该按钮,但仅使按钮操作影响与所述按钮对应的行。当前宏是:

    Sub RED()
    Rows("3:3").Select
    With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 10066431
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    End Sub

这对于第一个按钮很好,但当我尝试复制(下拉更多按钮)时,它们都只会影响原始代码(3:3)中所述的行。如果有,我无法找到一种方法来选择当前行,而不是定义的第3行。

如果表单控件命令按钮与行正确对齐,将以下两个宏放置在标准模块(如Module1)上

Sub RED(iRow As Long)
With Rows(iRow).Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 10066431
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
End Sub

Sub ColorRow()
Dim r As Long
Dim shp As Shape
Set shp = ActiveSheet.Shapes(Application.Caller)
r = shp.TopLeftCell.Row
Call RED(r)
End Sub
现在将宏ColorRow分配给所有COMMNAD按钮。因此,当您单击CommandButton时,代码将为CommandButton对齐的行着色