我为数据验证下拉列表创建了一个宏,该列表将使用值或黄色填充填充相邻两列中的单元格,具体取决于所选内容。下面是这张照片的图片:
当我从下拉列表中选择“是”后,在相邻的两个单元格中输入数据时,黄色填充保持不变。下面是这张照片的图片:
目标:一旦在其单元格中输入任何值或文本,我希望将黄色填充删除或“未填充”。
有没有办法在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
对此事我感激不尽!
答案 0 :(得分:1)
以为你有重复:How do I remove a fill color when data gets entered in cells from an adjacent drop down list?
现在看起来你想要条件格式,而不是仅仅关闭颜色。您可以使用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
这将完全替换您的代码以添加和删除颜色。
答案 1 :(得分:0)
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 A B 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
答案 2 :(得分:0)
此子将使用条件格式更改后填充,您只需更改范围以匹配您想要的。 您也可以在没有任何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