Reputation:
We have an invoice # format the is very strict and must match a certain format. We do this manually and I often get multiple variations so was trying to create data validation to control the entry of the invoice number.
Here’s the format:
YYYYMMDD-RNN
Need a formula to validate this.
Upvotes: 0
Views: 199
Reputation:
Try this UDF in a standard module code sheet.
Option Explicit
Function invoiceCheck(rng As Range) As Boolean
Dim tmp As String
tmp = rng.Value2
'check length
If Len(tmp) <> 12 Then Exit Function
'check valid date
If Not IsDate(Join(Array(Mid(tmp, 5, 2), Mid(tmp, 7, 2), Left(tmp, 4)), "/")) Then Exit Function
'check date is today or earlier
If CDate(Join(Array(Mid(tmp, 5, 2), Mid(tmp, 7, 2), Left(tmp, 4)), "/")) > Date Then Exit Function
'make sure there is a hyphen
tmp = Split(tmp, "-")(1)
invoiceCheck = IsNumeric(tmp)
End Function
Upvotes: 0
Reputation: 65
The following formula should work, but I couldn't insert it on custom data validation. I suspect there's a character limit. If anyone could shed a light, it would be nice.
=IF(AND(LEFT(A1;4)*1<=YEAR(TODAY());LEFT(A1;4)*1>=2000);IF(AND(MID(A1;5;2)*1>=1;MID(A1;5;2)*1<=12);IF(AND(MID(A1;7;2)*1>=1;MID(A1;5;2)*1<=30);IF(MID(A1;9;1)="-";IF(AND(MID(A1;10;1)*1>=0;MID(A1;10;1)*1<=9);IF(AND(MID(A1;11;2)*1>=1;MID(A1;11;2)*1<=99);TRUE;FALSE);FALSE);FALSE);FALSE);FALSE);FALSE)
P.S.: I assumed no invoices after the year 2000.
Upvotes: 1