一旦值或文本input到单元格中,如何删除单元格的背景颜色?

我为数据validation下拉列表创build了一个macros,该列表将填充相邻两列中的单元格,具有值或黄色填充(取决于所选内容)。 下面是这张照片的样子:

在这里输入图像说明

当我从下拉列表中select“是”后,在相邻的两个单元格中input数据时,黄色填充保持原位。 下面是这张照片的样子:

在这里输入图像说明

目标:一旦任何值或文本input到单元格中,我希望将黄色填充删除或“未填充”。

有没有办法在VBA中做到这一点? 我知道这是可行的条件格式,但我想看看这是否可行的VBA。

下面是我的代码,我鼓掌了:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Application.ScreenUpdating = False If Target.Count > 1 Then Target.Interior.ColorIndex = xlNone If Target.Count > 1 Then Exit Sub Select Case Target Case "YES" If Target = "YES" Then Target.Offset(0, 1).Interior.ColorIndex = 6 Target.Offset(0, 2).Interior.ColorIndex = 6 If Target.Offset(0, 1).Value = "NULL" Then Target.Offset(0, 1).ClearContents If Target.Offset(0, 2).Value = "NULL" Then Target.Offset(0, 2).ClearContents If Target.Offset(0, 1).Value = "NULL" Then Target.Offset(0, 1).Interior.Pattern = xlNone If Target.Offset(0, 2).Value = "NULL" Then Target.Offset(0, 2).Interior.Pattern = xlNone If Not Target.Cells.Count = 1 Then Exit Sub If Intersect(Target, Columns(2)) Is Nothing Then Exit Sub End If End If End If Case Else If Target = "NO" Then Target.Offset(0, 1) = "NULL" Target.Offset(0, 2) = "NULL" If Target.Offset(0, 1).Interior.ColorIndex = 6 Then Target.Offset(0, 1).Interior.Pattern = xlNone If Target.Offset(0, 2).Interior.ColorIndex = 6 Then Target.Offset(0, 2).Interior.Pattern = xlNone If Not Target.Cells.Count = 1 Then Exit Sub If Intersect(Target, Columns(2)) Is Nothing Then Exit Sub End If End If End If End Select End Sub 

我将不胜感激在这个问题上的任何帮助!

以为你有重复的: 当数据从相邻的下拉列表中input到单元格中时,如何去除填充颜色?

看起来你现在需要条件格式,而不是关掉颜色。 您可以使用Excel或VBA将其打开,如下所示:

 Sheets("NAME").Cells.FormatConditions.Delete With Sheets("NAME").Range("B2:C10000") .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(ISBLANK($B2),$A2=""Yes"")" With .FormatConditions(.FormatConditions.Count) .SetFirstPriority With .Interior .ColorIndex = 6 End With End With End With 

这将完全替代您的代码添加和删除颜色。

这个子将改变回填用条件格式,你可以改变范围来匹配你想要的。 你也可以做没有任何VBA的条件格式化,但我认为这是你想要的:

 Sub FormatForValues() Dim rngCells As Range Set rngCells = Range("D9:D16") rngCells.FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(D9))>0" rngCells.FormatConditions(rngCells.FormatConditions.Count).SetFirstPriority With rngCells.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With rngCells.FormatConditions(1).StopIfTrue = False End Sub 
  Option Explicit Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) '*** this assumes your yes no is in col A and you potentially have data in col b and col c *** '**declare your variables *** Dim Check_Word As String Dim Check_Data_ColB As String Dim Check_Data_ColC As String '** only run the code if something in col AB or C gets changed ***** If Target.Column = 1 Or Target.Column = 2 Or Target.Column = 3 Then '**** set check word to the value in col A *** Check_Word = Trim(UCase(Sh.Cells(Target.Row, 1).Text)) '**** set check_data_colB to the value in col B *** Check_Data_ColB = Trim(Sh.Cells(Target.Row, 2).Text) '**** set check_data_colC to the value in col C *** Check_Data_ColC = Trim(Sh.Cells(Target.Row, 3).Text) '*** If the check word is NO or the check word is yes but there is text in col B or C then clear the cells colour *** If Check_Word = "NO" Or (Check_Word = "YES" And (Check_Data_ColB <> "" Or Check_Data_ColC <> "")) Then '*** all other situations result in the cells getting filled in with Yellow **** Sh.Cells(Target.Row, 2).Interior.ColorIndex = 0 Sh.Cells(Target.Row, 3).Interior.ColorIndex = 0 Else '*** all other situations result in the cells getting filled in with Yellow **** Sh.Cells(Target.Row, 2).Interior.Color = vbYellow Sh.Cells(Target.Row, 3).Interior.Color = vbYellow End If End If End Sub