Solved

VBA Test For Valid Number Format

Posted on 2016-09-21
6
68 Views
Last Modified: 2016-09-21
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.
0
Comment
Question by:kgerb
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
Shahid Thaika earned 500 total points
ID: 41808944
In VBA, you can use in-built functions such as...

IsNumeric("666")

or

IsDate("08/22/1985")

However, it will only work with proper formatting. i.e. use of ' / '.

If your text contains ' . ' due to some reason, you can do something like...

IsDate(Replace("08.22.1985", ".", "/"))
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41809391
valid number format
The different format string options depend on the data being formatted.  Your first example was not a number format, rather a date format.  Please explain your problem in greater detail.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41809394
"1.1.2..3kklksjlkj" is not
Tested 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

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Author Comment

by:kgerb
ID: 41809510
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...
?Format("1/1/2000","1.1.2..3kklksjlkj")
136526.1.2..3kklksjlkj

Open in new window

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
0
 
LVL 45

Expert Comment

by:aikimark
ID: 41809582
use the IsDate() function
0
 
LVL 12

Author Closing Comment

by:kgerb
ID: 41809586
Thanks Shahid!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question