kgerb
asked on
VBA Test For Valid Number Format
Hello Everyone,
I am looking for a function that when passed a number format as a string (e.g. "mm/dd/yyyy") returns True or False depending on whether the string is indeed a valid number format. Obviously "mm/dd/yyyy" is valid but "1.1.2..3kklksjlkj" is not. Does anyone have this already written up? I'm assuming this will require the use of regular expressions and I have never swam in that end of the pool.
Many thanks!
Kyle
P.S. Please do not post solutions using error checking. I know that is an option.
I am looking for a function that when passed a number format as a string (e.g. "mm/dd/yyyy") returns True or False depending on whether the string is indeed a valid number format. Obviously "mm/dd/yyyy" is valid but "1.1.2..3kklksjlkj" is not. Does anyone have this already written up? I'm assuming this will require the use of regular expressions and I have never swam in that end of the pool.
Many thanks!
Kyle
P.S. Please do not post solutions using error checking. I know that is an option.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"1.1.2..3kklksjlkj" is notTested this 'not valid' format string In the immediate window of a VBA application and got a result with no error message.
?format(789,"1.1.2..3kklksjlkj")
1789.1.2..3kklksjlkj
ASKER
Hi Mark,
Thanks for your comment. In this particular instance I am dealing with dates. I should have been more specific in my original post. You are correct, running this from the immediate window does not throw an error...
At any rate, my little project is attached. It's a simple calendar add-in that will let the user graphically pick a date and then enter it into the selected cell. There is a text box to control the date format. My goal is to validate the text entered into that box before I attempt to modify the cell format and write the date to the sheet. Thus, the need to test a text string as being a valid date format. Currently, I'm using Shahid's solution. If I need to go with this I can, but I was hoping to gain a little more understanding of what is allowed and what it not.
Thanks,
Kyle
Calendar.xlam
Thanks for your comment. In this particular instance I am dealing with dates. I should have been more specific in my original post. You are correct, running this from the immediate window does not throw an error...
?Format("1/1/2000","1.1.2..3kklksjlkj")
136526.1.2..3kklksjlkj
However, entering this as a custom format in the Format Cells dialogue does produce an error.At any rate, my little project is attached. It's a simple calendar add-in that will let the user graphically pick a date and then enter it into the selected cell. There is a text box to control the date format. My goal is to validate the text entered into that box before I attempt to modify the cell format and write the date to the sheet. Thus, the need to test a text string as being a valid date format. Currently, I'm using Shahid's solution. If I need to go with this I can, but I was hoping to gain a little more understanding of what is allowed and what it not.
Thanks,
Kyle
Calendar.xlam
use the IsDate() function
ASKER
Thanks Shahid!