仅当下一个colmn字段为空时,VBA excel更新字段

时间:2019-03-05 15:06:37

标签: excel vba

如果在I列中写入了任何值,则下面输入的代码会自动在J列中日期,我们如何添加条件:如果J已经有一个值,则不执行任何操作?

Private Sub Worksheet_Change(ByVal Target As Range)


If Not Intersect(Target, Range("I:I")) Is Nothing Then
      Range("J" & Target.Row).Value = Now

End If

End Sub

1 个答案:

答案 0 :(得分:1)

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng as Range, c As Range
    Set rng = Application.Intersect(Target, Me.Range("I:I"))
    If Not rng Is Nothing Then
        for each c in rng.cells
            with c.offset(0, 1)
                if len(.value) = 0 then .value = now
            end with
        next c
    End If   

End Sub