[英]Applying date validation to Excel cell from VB.Net
I am working on applying date validation to excel cell(range) from VB.NET. 我正在将日期验证应用于VB.NET中的excel单元格(范围)。
Private Sub DateValidExcelRule(ByVal worksheet As SpreadsheetGear.IWorksheet, ByVal DateRange As String)
Dim dt As Date = CDate("1/1/1900")
worksheet.Range(DateRange).Validation.Delete()
worksheet.Range(DateRange).Validation.Add(ValidationType.Date, ValidationAlertStyle.Stop, ValidationOperator.Greater, dt, Nothing)
worksheet.Range(DateRange).NumberFormat = "mm/dd/yyyy"
worksheet.Range(DateRange).Validation.IgnoreBlank = True
worksheet.Range(DateRange).Validation.InputTitle = "Excel Validation"
worksheet.Range(DateRange).Validation.ErrorTitle = "Error in Date"
worksheet.Range(DateRange).Validation.InputMessage = "Note: only date values here"
worksheet.Range(DateRange).Validation.ErrorMessage = "Enter valid Date"
worksheet.Range(DateRange).Validation.ShowInputMessage = True
worksheet.Range(DateRange).Validation.ShowError = True
End Sub
Problem: 问题:
When I enter 4 digits whole number 7777
it is validating correctly showing message Enter Valid Date
, but when I enter 5 digits 77777
it is accepting the value and converting it to 12/10/2112
value and not showing any error message. 当我输入4位整数
7777
它正在正确验证并显示消息Enter Valid Date
,但是当我输入5位数字77777
它正在接受该值并将其转换为12/10/2112
值,并且未显示任何错误消息。
Here all I want to do is I want to validate the cell value to any date format mm/dd/yyyy
. 在这里,我要做的就是将单元格值验证为任何日期格式
mm/dd/yyyy
。
Please let me know, am I going in the right way? 请让我知道,我走的路正确吗?
Excel stores dates as serial dates (the number of days since 1/0/1900), so if you take 77777 days and add it to that date it starts you get a valid date. Excel将日期存储为序列日期(自1/0/1900以来的天数),因此,如果您花费77777天并将其添加到该日期,则它将获得一个有效日期。 12/10/2112
12/10/2112
I am not sure how or even if you can accomplish exactly what you want forcing the input to be in that specific format in excel. 我不确定如何甚至可以完全完成您想要在excel中以特定格式强制输入的内容。 (Without using a date control) But that is the why at least.
(不使用日期控件)但这至少是原因。
Here are a couple links I have found to be useful with dates/times in excel. 这是我发现对Excel中日期/时间有用的几个链接。
http://www.cpearson.com/excel/datetime.htm http://www.cpearson.com/excel/datetime.htm
http://dmcritchie.mvps.org/excel/datetime.htm http://dmcritchie.mvps.org/excel/datetime.htm
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.