Solved

VBA Test For Valid Number Format

Posted on 2016-09-21
6
44 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now