使用宏代码创建单元格颜色时删除单元格颜色

Remove Cell color when cell color is created with a Macro Code

我创建了一个宏来在进行更改时将单元格更改为黄色(顶部宏)。我现在想创建一个代码,这样我就可以创建一个按钮来单击以删除使用顶部宏创建的所有黄色。

我能够找到底部代码,它确实将手动突出显示的单元格从黄色变回白色,但没有从我的顶部宏中将单元格变为黄色。

以下是我使用的格式:

要在进行更改时创建黄色:

'Highlight cells yellow if change occurs

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

If Target.Cells.Count > 1 Then Exit Sub
    Target.Interior.ColorIndex = 6
End Sub

删除黄色突出显示(仅适用于手动更改 - 不适用于宏)

Sub RemoveYellowFillColor()
Dim cell As Range

'Optimize Code
Application.ScreenUpdating = False

'Ensure Cell Range Is Selected
If TypeName(Selection) <> "Range" Then
MsgBox ("A2:Z1000")
Exit Sub
End If

'Loop Through Each Cell
For Each cell In Selection.Cells
If cell.Interior.Color = vbYellow Then
  cell.Interior.Color = xlNone
End If
Next

End Sub

这是我的第 1 次回答:

如评论中所述,在 Workbook_SheetChange 中从 Target.Interior.ColorIndex = 6 更改为 Target.Interior.ColorIndex = vbYellow

然后按如下方式更新您的宏:

Sub RemoveYellowFillColor()
Dim ws As Worksheet, cell As Range

'Optimize Code
Application.ScreenUpdating = False

'Loop Through Each Cell
For Each ws In Worksheets
For Each cell In ws.UsedRange.Cells
If cell.Interior.Color = vbYellow Then cell.Interior.Color = xlNone
Next cell
Next ws

Application.ScreenUpdating = True
End Sub

在 运行 这个宏之后,vbYellow 将从工作簿中所有工作表的所有单元格中删除填充。