2017-12-18 53 views
-1

我有一個電子表格,其中包含銷售清單。更新第一個單元格後強制用戶更新另一個單元格

當用戶將「狀態」列設置爲「已關閉」時,我想強制他們在「已關閉日期」列中輸入日期。

所以我有這個;

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

任何想法?

奧利

+1

你有什麼企圖?這不是一個代碼寫入服務。 –

+0

如果我誠實,我甚至不知道從哪裏開始。我以前沒有做過很多宏。希望得到一些提示或指導/建議。 –

+1

你可以調查Change事件,但是如果你想強制這個要求,BeforeSave更可能。 –

回答

1

按什麼安迪說,SO不是代碼寫作服務。但考慮到你不知道從哪裏開始的想法,併爲您提供一個起點,請按照下列步驟......

Right clickSheet Tab - 在下面給出入opened code windowsave您的工作簿作爲Macro-Enabled Workbook>View codepaste the code

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 
相關問題