VBA - 细胞变化

时间:2016-06-18 03:04:10

标签: excel vba excel-vba

我需要用户在列C中插入是/否。如果不是,则下一个单元格应显示N / A并填写为灰色。如果是,则下一个单元格应突出显示为黄色,并允许用户填写该单元格。

代码如下,但如果某个单元格没有,然后更改为“是”,则下一个单元格不会从N / A更改为高亮显示。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    If Target.Column = 3 Then
        Set Cell = Target.Offset(0, 1)
        If Len(Target.Value) = 0 Then
            Cell.Validation.Delete
            Cell.Value = vbNullString
        Else
            If Target.Value = "Yes" Then
                With Cell.Validation
                    Cell.Interior.ColorIndex = 36
                End With
            ElseIf Target.Value = "No" Then
                Cell.Validation.Delete
                Cell.Value = "N/A"
            Else
                MsgBox "Input only Yes or No."
                Target.ClearContents
                Cell.Validation.Delete
            End If
        End If
    End If
    End Sub

3 个答案:

答案 0 :(得分:1)

这是因为你没有在if块中为yes条件添加一行来更改相邻单元格的值。此外,对于no block,您可能希望将单元格的颜色更改为白色,否则在之前为yes时它将保持黄色。下面的代码应该达到你想要的效果。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    If Target.Column = 3 Then
        Set Cell = Target.Offset(0, 1)
        If Len(Target.Value) = 0 Then
            Cell.Validation.Delete
            Cell.Value = vbNullString
        Else
            If Target.Value = "Yes" Then
                With Cell.Validation
                    Cell.Interior.ColorIndex = 36
                End With
                Cell.Value = ""
            ElseIf Target.Value = "No" Then
                Cell.Validation.Delete
                Cell.Value = "N/A"
                Cell.Interior.ColorIndex = 0
            Else
                MsgBox "Input only Yes or No."
                Target.ClearContents
                Cell.Validation.Delete
            End If
        End If
    End If
    End Sub

答案 1 :(得分:0)

If target.value = "yes" then
    cell.validation.delete
    cell.interior.color = vbyellow
    cell.clearcontents
end if

答案 2 :(得分:0)

  • 清除单元格的内容时,您需要打开EnableEvents。这样就不会再触发Worksheet_Change。否则你会陷入困境。
  • 您没有使用Excel内置的单元格验证进行验证
  • 您可以并且应该使用Excel内置的单元格验证和条件格式来完成您想要做的事情。

    • 如果用户影响多个单元格的单元格值,此方法将失败,单元格验证和条件格式仍将正常工作。

    Private Sub Worksheet_Change(ByVal Target As Range)     昏暗的细胞作为范围

    If Target.Column = 3 Then
        Application.EnableEvents = False
        Set Cell = Target.Offset(0, 1)
        With Cell.Interior
            If Target.Value = "Yes" Then
                'Change Cell Color to Yellow
                .ColorIndex = 36
            ElseIf Target.Value = "No" Then
                'Change Cell Color to Grey 
                ' Insert Grey Color Change
                Cell.Value = "N/A"
            Else
                Target.ClearContents
                Cell.ClearContents
                With Cell.Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
                MsgBox "Input only Yes or No."
            End If
        End With
        Application.EnableEvents = True
    End If
    

    End Sub

您可以使用MsgBox询问他们是否希望该值为yes或no。

iResponse = MsgBox("Input only Yes or No.", vbYesNoCancel)
Select Case iResponse
    Case vbYes
        Target.Value = "Yes"
    Case vbNo
        Target.Value = "Yes"
    Case Else
        'You need to turn of Events when clearing the cell
        'So that the Worksheet_Change won't fire again
        Application.EnableEvents = False
        Target.ClearContents
        Cell.ClearContents

End Select