FotoDJ
FotoDJ

Reputation: 351

VBA excel update field only if the next colmn field is empty

Code below input automatically date in the column J if any value is written in column I, how can we add condition: if J has already a value do nothing?

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

Upvotes: 0

Views: 61

Answers (1)

Tim Williams
Tim Williams

Reputation: 166790

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

Upvotes: 1

Related Questions