根据另外两列更改两列中的值

时间:2016-12-29 15:09:50

标签: excel vba excel-vba

我拼凑了这段代码,根据另外两个单元改变了两个单元格的值。如何将此代码应用于整个列范围?

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ErrorHandler

    If Range("N6").Value Like "FINISH" And Not Range("CL6").Value Like "BK WALL" Or Not Range("CL6").Value Like "INTG" Then
        Application.EnableEvents = False
        Range("CH6").Value = "Y"
End If
        Application.EnableEvents = True
   If Not Range("N6").Value Like "FINISH" Or Range("CL6").Value Like "BK WALL" Or Range("CL6").Value Like "INTG" Then
        Application.EnableEvents = False
        Range("CH6").Value = "X"
        Range("CO6").Value = ""
End If

ErrorExit:
    Application.EnableEvents = True
Exit Sub
ErrorHandler:
    Debug.Print Err.Number & vbNewLine & Err.Description
    Resume ErrorExit
On Error GoTo ErrorHandler


End Sub

1 个答案:

答案 0 :(得分:0)

类似的东西:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim dStart As Double, dFinish As Double, d As Double

On Error GoTo ErrorHandler

dStart = 6
dFinish = Range("N" & dStart).CurrentRegion.Rows.Count + dStart - 1
'You could just hard-code in dFinish = 45 here if you want...


For d = dStart To dFinish

    If Range("N" & d).Value Like "FINISH" And Not Range("CL" & d).Value Like "BK WALL" Or Not Range("CL6").Value Like "INTG" Then
        Application.EnableEvents = False
        Range("CH" & d).Value = "Y"
    End If

    Application.EnableEvents = True

    If Not Range("N" & d).Value Like "FINISH" Or Range("CL" & d).Value Like "BK WALL" Or Range("CL" & d).Value Like "INTG" Then
        Application.EnableEvents = False
        Range("CH" & d).Value = "X"
        Range("CO" & d).Value = ""
    End If

Next

ErrorExit:
    Application.EnableEvents = True
Exit Sub
ErrorHandler:
    Debug.Print Err.Number & vbNewLine & Err.Description
    Resume ErrorExit
On Error GoTo ErrorHandler


End Sub