excel 删除名称时的删除日期

ql3eal8s  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(156)

我有VBA代码,自动输入日期+3天在特定的单元格时,在另一个单元格中输入一个名称。见下面的代码。我需要的是代码添加到这将删除日期时,名称被删除。

'When name entered in H34 date + 3 days is entered in H37

Private Sub Worksheet_Change(ByVal Target As Range)
    'Auto fills Shipment Required By date in H37 When Name
    'entered in H34 under Submitted BY
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("H34:H34")) Is Nothing Then
        With Target(4, 1)     ' Adjust numbers to match cell for date to go into -currently H37
            .Value = Date + 3 'this will use date when name is entered into Order Submitted By cell  and add 3 days
                              'Add .EntireColumn.AutoFit to fit data to cells. Does not work on merged cells
        End With
    End If
End Sub

从H34中删除名称时,需要添加代码以清除H37中的日期
已经尝试过这个,但它不工作。不同的代码建设从我原来的

If Sheets("OrderForm").Range("H34") = "" Then
    Sheets("OrderForm").Range("H37").ClearContents
End if
4zcjmb1e

4zcjmb1e1#

此示例显示快速检查单元格是否有名称:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    'Auto fills Shipment Required By date in H37 When Name
    'entered in H34 under Submitted BY
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("H34:H34")) Is Nothing Then
        '--- check if the name is there or not
        If Not IsEmpty(Target) Then
            '--- we have a name, so adjust numbers to match
            '    cell for date to go into -currently H37
            With Target(4, 1)
                .Value = Date + 3
            End With
        Else
            '--- name has been deleted, so remove the date
            With Target(4, 1)
                .Value = vbNullString
            End With
        End If
    End If
End Sub

相关问题