Change by val target as range to ignore blank and

2019-08-29 03:58发布

Private Sub Worksheet_Change(ByVal Target As Range)

Dim c As Range

For Each c In Target.Cells
   **If c.Value <> Empty Or c.Value = 0 Then
   End If**
   If c.Column = 11 Then
   c.Offset(0, -1).Value = Now()

 End If
 Next c

End Sub

I have the above code working well except I am trying to add the bolded code to ignore any blank cells (could also be the option of ignoring 0 value cells, but not necessary).

Thanks

2条回答
看我几分像从前
2楼-- · 2019-08-29 04:40

You seem to have your two arguments going in different ways, in that your lumping together <> Empty and =0 in the same test.

At any rate, this makes the change if there's something in the cell besides 0 and, as a bonus, clears the change if it is empty or 0.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range

Application.EnableEvents = False
For Each c In Target.Cells
    If c.Column = 11 Then
        If c.Value = "" Or c.Value = 0 Then
            c.Offset(0, -1).ClearContents
        Else
            c.Offset(0, -1).Value = Now()
        End If
    End If
Next c
Application.EnableEvents = True
End Sub
查看更多
我命由我不由天
3楼-- · 2019-08-29 04:45
If c.Value != ""

should work for blank cells.

At least it worked here.

As for ignoring the value 0, couldn't you just change the if clause to if c.Value > 0?

查看更多
登录 后发表回答