简体   繁体   English

Excel VBA 添加日期小于或等于动态今天日期的数据验证

[英]Excel VBA add data validation with date less or equal to dynamic todays date

As in the title Im trying to add data validation with date less or equal than todays date.如标题所示,我正在尝试添加日期小于或等于今天的数据验证。 By todays date I refer to dynamically check for date when sheet is edited.到今天为止,我指的是在编辑工作表时动态检查日期。 Below is my code:下面是我的代码:

            With Selection.Validation
                .Delete
                .Add Type:=xlValidateDate, AlertStyle:=xlValidAlertStop, Operator:=xlLessEqual, Formula1:="=NOW()"
                .ErrorMessage = "Wprowadź datę w formacie dd.mm.rrrr"
            End With

I get here runtime 1004 error with this line:我在这行出现运行时 1004 错误:

.Add Type:=xlValidateDate, AlertStyle:=xlValidAlertStop, Operator:=xlLessEqual, Formula1:="=NOW()"

I've been trying to use =TODAY insted of =NOW but output was the same.我一直在尝试使用=TODAY代替=NOW ,但 output 是相同的。

The code works fine but some sheets in my workbook were protected.代码工作正常,但我工作簿中的一些工作表受到保护。 When I unprotected them code worked perfectly.当我解除对它们的保护时,代码可以完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM