将VBA代码添加到日期选择器以控制允许的日期

时间:2015-04-14 10:42:51

标签: excel vba datepicker

我使用以下公式在我的单元格中嵌入日期选择器。

=EMBED("LDDATETIME.LDDateCtrl.1","")

我的问题是如何控制用户可以选择的日期。例如,我不希望他们能够仅选择过去日期之后的任何日期。

我试过这个,但它给了我一个未定义的对象错误:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If LDDate1 > Date Then
MsgBox "Error"
End If
End Sub

请有人告诉我这里出错了吗?

1 个答案:

答案 0 :(得分:0)

您的代码需要跟踪工作表中的更改...试试这个

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Value > Date And Target.Column = 3 Then 'The Second part Target.Column assumes that you have the Dates that are being changes in the second column
    MsgBox "Error"
  End If
End Sub