【问题标题】:if Cell is Blank then clear content of another cell Code in VBA如果单元格为空白,则清除 VBA 中另一个单元格代码的内容
【发布时间】:2020-02-17 22:22:42
【问题描述】:

在我的工作表中,当我更新 D 列中的单元格时,C 列中的单元格将显示更新日期,现在我需要如果我删除 D 单元格中的信息来删除 C 单元格中的信息,而不是像 VBA 代码那样的公式 代码如下:

 `Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Column <> 4 Then Exit Sub
  If Target.Cells.Count > 1 Then Exit Sub
   With Target.Offset(0, -1)
    .Value = Now
     .NumberFormat = " MM/DD/YY hh:mm Am/PM"
  End With

   Dim RangeA As Range
   Set RangeA = Range("D10:D10000")
    If Application.CountBlank(RangeA) = RangeA.Cells.Count Then
    Range("C10:C10000").ClearContents
     End If
     End Sub`

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我建议添加if-statement 来支持您的活动,例如:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column <> 4 Then Exit Sub
        If Target.Cells.Count > 1 Then Exit Sub
        If Target.Value <> "" then
            With Target.Offset(0, -1) 
                .Value = Now
                .NumberFormat = " MM/DD/YY hh:mm Am/PM"
            End With
        Else
            Target.Offset(,-1).ClearContents
        End If
    End Sub
    

    【讨论】:

    • @Aboudezoua 精彩;如果这已经解决了您的问题,请标记为您的答案(我的帖子左侧的复选标记)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多