gpt4 book ai didi

excel - 如果填充颜色为 X 的单元格更改为无填充颜色,则将单元格填充颜色返回为 X

转载 作者:行者123 更新时间:2023-12-04 20:56:39 25 4
gpt4 key购买 nike

我在 Excel 中有一个包含日期的表格。表格的记录链接到日历(在另一张纸上),因此如果您单击表格中的日期,您将被带到日历中该日期的单元格。在我的日历表上,我有以下 VBA 将该表的事件单元格的填充颜色更改为黄色。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Dim cell As Range
'Turn off ScreenUpdating (speeds up code)
Application.ScreenUpdating = False
'Loop through each cell in the ActiveSheet
For Each cell In ActiveSheet.UsedRange
'Check for a specific fill color
If cell.Interior.Color = RGB(255, 255, 0) Then
'Remove Fill Color
cell.Interior.Color = xlNone
End If
Next cell
' Highlight the active cell
Target.Interior.ColorIndex = 6
Application.ScreenUpdating = True
End Sub

如果用户激活日历表上的另一个单元格,该单元格最初包含填充颜色,它将清除该单元格的原始颜色。

我希望通过此代码将工作表上的单元格从填充颜色 X(在我的情况下为紫色)更改为无填充颜色,而是保持用户设置的填充颜色 X。

我基本上需要在 Excel 中填充颜色图层。

最佳答案

请检查这个(我使用 2 个范围(a2,a3)来保存以前的情况,你可以选择任何你想要的:

 Option Explicit




Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Dim cell As Range
Dim i As String
Dim i1 As Long

On Error Resume Next

i = Range("a3").Value
i1 = Range("a2").Value


'Turn off ScreenUpdating (speeds up code)
Application.ScreenUpdating = False

'Loop through each cell in the ActiveSheet
For Each cell In ActiveSheet.UsedRange

'Check for a specific fill color
If cell.Interior.Color = RGB(255, 255, 0) Then

'Remove Fill Color
cell.Interior.Color = xlNone

End If

Next cell

Range(i).Interior.Color = i1

' Highlight the active cell
' If Target.Interior.ColorIndex = -4142 Then
Range("a3").Value = Target.Address
Range("a2").Value = Target.Interior.Color
Target.Interior.ColorIndex = 6

' End If

Application.ScreenUpdating = True
End Sub

关于excel - 如果填充颜色为 X 的单元格更改为无填充颜色,则将单元格填充颜色返回为 X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46366050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com