我是初学者 - 试图缩短这段代码

时间:2016-06-18 12:27:05

标签: excel vba excel-vba

我希望C列中的“是/否”在列d-h中引发一系列事情: 如果没有单元格为灰色并显示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.ClearContents
        Cell.Interior.ColorIndex = 2
        Cell.Value = vbNullString
    Else
        If Target.Value = "Yes" Then
            Cell.ClearContents
            Cell.Interior.ColorIndex = 36

        ElseIf Target.Value = "No" Then
            Cell.ClearContents
            Cell.Interior.ColorIndex = 16
            Cell.Value = "N/A"
        End If
    End If
End If

If Target.Column = 3 Then
Set Cell = Target.Offset(0, 2)
    If Len(Target.Value) = 0 Then
        Cell.ClearContents
        Cell.Validation.Delete
        Cell.Interior.ColorIndex = 2
        Cell.Value = vbNullString
Else
        If Target.Value = "Yes" Then
            Cell.ClearContents
            Cell.Interior.ColorIndex = 36
            With Cell.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="=Numbers"
            End With
ElseIf Target.Value = "No" Then
            Cell.ClearContents
            Cell.Validation.Delete
            Cell.Interior.ColorIndex = 16
            Cell.Value = "N/A"
        End If
    End If
End If

If Target.Column = 3 Then

Set Cell = Target.Offset(0, 3) 

注意:这适用于F列,但我对G列也需要相同

If Len(Target.Value) = 0 Then
        Cell.ClearContents
        Cell.Validation.Delete
        Cell.Interior.ColorIndex = 2
        Cell.Value = vbNullString
Else
        If Target.Value = "Yes" Then
            Cell.ClearContents
            Cell.Interior.ColorIndex = 36
            With Cell.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="=Numbers"
            End With
ElseIf Target.Value = "No" Then
            Cell.ClearContents
            Cell.Validation.Delete
            Cell.Interior.ColorIndex = 16
            Cell.Value = "N/A"
        End If
    End If
End If

End Sub

1 个答案:

答案 0 :(得分:1)

你可以做那样的事情,不重复代码。您将在Union Range中指定要检查的列,如果与该范围相交,则只检查一次。

然后,如果你必须删除指定的单元格偏移量,只需将其添加到if中,Cell.Validation将删除单元格

Option Explicit

Private Sub Worksheet_Change(ByVal target As Range)
    Dim rngToCheck As Range
    Set rngToCheck = Union(ActiveSheet.Columns(3), ActiveSheet.Columns(4))
    If Not Intersect(target, rngToCheck) Is Nothing Then
        FormatCell target
    End If
End Sub

Sub FormatCell(ByRef target As Range)
    Dim offRange As Integer, cell As Range

    For offRange = 1 To 3
        Set cell = target.Offset(0, offRange)
        If Len(target.Value) = 0 Then
            cell.ClearContents
            cell.Interior.ColorIndex = 2
            cell.Value = vbNullString
        Else
            If target.Value = "Yes" Then
                cell.ClearContents
                cell.Interior.ColorIndex = 36
                If offRange = 2 Then
                    With cell.Validation
                        .Delete
                        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                        Operator:=xlBetween, Formula1:="=Numbers"
                    End With
                End If
            ElseIf target.Value = "No" Then
                If offRange = 2 Then cell.Validation.Delete
                cell.ClearContents
                cell.Interior.ColorIndex = 16
                cell.Value = "N/A"
            End If
        End If
    Next offRange
End Sub

一个建议,从不在事件中编写代码,只是让事件调用另一个函数将必要的东西传递给该函数,它应该是清楚的,你必须在函数中指定你想要做什么!

您可以这样阅读我的代码:

工作表更改? 创建一个要检查的范围,我们必须观察第3列和第4列。

检查更改的单元格是否在该范围内(交叉方法)。

如果是,请将范围传递给FormatCell函数,让他做他想做的事。