Change by val target as range to ignore blank and 0 value cells

混江龙づ霸主 提交于 2019-12-12 04:35:01

问题


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


回答1:


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



回答2:


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?



来源:https://stackoverflow.com/questions/17177581/change-by-val-target-as-range-to-ignore-blank-and-0-value-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!