vba 激活单元格时将字体颜色更改为黑色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18279332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:48:01  来源:igfitidea点击:

Change font color to black when cell is activated

vba

提问by halcyon27

I have a cell formatted with red font. I'd like for the format to change to black font when the user clicks on the cell, or navigates to it with the keyboard.

我有一个用红色字体格式化的单元格。当用户单击单元格或使用键盘导航到它时,我希望格式更改为黑色字体。

The cell is "Q15". I'd like the code to apply to all instances of "Q15" within the workbook--regardless of which sheet.

电池是“Q15”。我希望代码适用于工作簿中“Q15”的所有实例——无论是哪一张。

I have entered the following in "ThisWorkbook". How do I specify that I am referring to "Q15" in all worksheets?

我在“ThisWorkbook”中输入了以下内容。如何在所有工作表中指定我指的是“Q15”?

Private Sub Worksheet_Change(ByVal Target as Range) 
    Target.Font.ColorIndex = 1
End Sub

回答by Kazimierz Jawor

ThisWorkbook moduleis correct place where you need to add your code.

ThisWorkbook module是您需要添加代码的正确位置。

However, you need to use different kind of event. Use the one in the following (complete) code:

但是,您需要使用不同类型的事件。使用以下(完整)代码中的一个:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    If Target.Address = "$Q" Then

        Target.Font.ColorIndex = 1

    End If
End Sub