Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Vba 如果用户更改单元格的内容,请更改单元格的颜色_Vba_Excel - Fatal编程技术网

Vba 如果用户更改单元格的内容,请更改单元格的颜色

Vba 如果用户更改单元格的内容,请更改单元格的颜色,vba,excel,Vba,Excel,如果用户更改单元格,则尝试更改单元格的颜色。此时,它会在我更改某些内容后更改我选择的下一个单元格 Private Sub Worksheet_Change(ByVal Target As Range) If Environ("Username") = "HelloWorld" Then With ActiveCell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .

如果用户更改单元格,则尝试更改单元格的颜色。此时,它会在我更改某些内容后更改我选择的下一个单元格

Private Sub Worksheet_Change(ByVal Target As Range)
If Environ("Username") = "HelloWorld" Then
    With ActiveCell.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 7195899
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End If
End Sub
您只需将“ActiveCell.Interior”更改为“Target.Interior”:

这是因为此子单元是在您更改任何单元格后触发的,因此引用的“ActiveCell”与“Target”不同


快乐编码

使用ActiveCell.Interior将
更改为使用Target.Interior将
ActiveCell
作为
Target
传递到
工作表\u change
Event哇,这是一个简单的解决方案!我唯一的问题是它现在删除了撤消的功能据我所知,所有宏都删除了撤消的功能,是否只撤消单元格格式?也许您可以在代码中添加一些条件,以防止某些您希望避免的事情发生,因此无需撤消。或者,您可以使用带有公式的条件格式作为条件,并将用户名保存在“VeryHidden”工作表中,以便用户无法修改它。然后你可以将这个条件引用到用户名上。是的,如果你不能撤消,那么用户友好性就会大大降低。如果用户出错,那么他们就不能按撤消。
Private Sub Worksheet_Change(ByVal Target As Range)
If Environ("Username") = "HelloWorld" Then
    With Target.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 7195899
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End If
End Sub