更新第一个单元格

时间:2017-12-18 10:32:43

标签: excel vba excel-vba

我有一个电子表格,上面有销售清单。

当用户设置"状态"列到"已关闭"我想强迫他们在"关闭日期"中输入日期。柱。

所以我有这个;

A1 (Status), B1 (Closed Date)
Open, <blank>
Open, <blank>
Closed, 1/1/2018

有什么想法吗?

奥利

1 个答案:

答案 0 :(得分:1)

根据 Andy 所说的, SO 不是代码编写服务。但是考虑到你不知道从哪里开始并为你提供一个起点,请遵循以下步骤......

Right click上的

Sheet Tab - &gt; View codepaste the code下面的opened code windowsave工作簿为Macro-Enabled Workbook

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Target.Column = 1 And Target.Row > 1 Then
    If Target <> "" And LCase(Target.Value) = "closed" Then
        With Target.Offset(0, 1)
            .Select
            On Error Resume Next
            .Comment.Delete
            On Error GoTo 0
            .AddComment.Text "Please enter the Closed Date"
            .Comment.Visible = True
        End With
    Else
        On Error Resume Next
        Target.Offset(0, 1).Comment.Delete
        On Error GoTo 0
    End If
ElseIf Target.Column = 2 And Target.Row > 1 Then
    If LCase(Target.Offset(0, -1)) = "closed" And IsDate(Target) Then
        Target.Comment.Delete
    End If
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim x
Dim i As Long, lr As Long

Application.ScreenUpdating = False
Application.EnableEvents = False

lr = Cells(Rows.Count, 1).End(xlUp).Row
x = Range("A2:B" & lr)

If Target <> "" Then
    Application.EnableEvents = True
    Exit Sub
End If

For i = 1 To UBound(x, 1)
    If LCase(x(i, 1)) = "closed" And Not IsDate(x(i, 2)) Then
        Cells(i + 1, 2).Select
        Exit For
    End If
Next i

Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub