Solved

VBA Test For Valid Number Format

Posted on 2016-09-21
6
60 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

803 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