只有当多个单元格发生变化时才会得到结果 VBA

问题描述 投票:0回答:1

我有一段代码,当单元格的值发生变化时,可以在第3列中填入日期。Range("E:J"). 工作正常,但我还想在第11列中显示第4列的值(第4列是隐藏的),只有当所有单元格在 Range(E:J) 被填满。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Intersect(Target, Range("E:J")) Is Nothing Then Exit Sub

    Application.EnableEvents = False
    If Target.Value <> vbNullString Then
        Target.Offset(0, 3 - Target.Column).Value = Date
        Target.Offset(0, 3 - Target.Column).NumberFormat = "dd/mmm/yyyy"
    Else
        Target.Offset(0, 3 - Target.Column).ClearContents
    End If
    Application.EnableEvents = True
End Sub

如果有任何帮助,将非常感谢。

谢谢。

excel vba excel-vba
1个回答
0
投票

考虑一下。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim tc As Long, r As Range, tr As Long
    Dim wf As WorksheetFunction

    Set wf = Application.WorksheetFunction
    If Target.Count > 1 Then Exit Sub
    tc = Target.Column
    tr = Target.Row
    If Intersect(Target, Range("E:J")) Is Nothing Then Exit Sub
    Set r = Range(Cells(tr, "E"), Cells(tr, "J"))

     Application.EnableEvents = False
        If Target.Value <> vbNullString Then
            Target.Offset(0, 3 - tc).Value = Date
            Target.Offset(0, 3 - tc).NumberFormat = "dd/mmm/yyyy"
        Else
            Target.Offset(0, 3 - tc).ClearContents
        End If

        If wf.CountA(r) = 6 Then
            Cells(tr, 11).Value = Cells(tr, 4).Value
        End If

     Application.EnableEvents = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.