+ Reply to Thread
Results 1 to 7 of 7

Validate Numeric Input

  1. #1
    Registered User
    Join Date
    03-08-2021
    Location
    Hamilton, New Zealand
    MS-Off Ver
    2008
    Posts
    3

    Validate Numeric Input

    Hi

    I'm just learning am I'm stuck with validating a UserForm input to ensure a numeric entry in exactly a certain way e.g. 140.235678 or -121.222233 (i.e. 3 places followed by 6 decimal places and could be negative). This is my code below which I know is rubbish but hopefully someone will be kind enough to help. I'm also trying to return an error message to the user if the syntax is not right and also to populate the value to a cell if correct.

    Many thanks
    Dave

    Please Login or Register  to view this content.
    Last edited by Glenn Kennedy; 03-08-2021 at 04:48 AM.

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,022

    Re: Validate Numeric Input

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Since you are new here, on this occasion I have done it for you).
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,908

    Re: Validate Numeric Input

    Can there be leading/trailing zeroes and, if so, should they appear in the cell? If they should, do they need to actually be stored in the cell, or can they just be displayed that way?
    Rory

  4. #4
    Registered User
    Join Date
    03-08-2021
    Location
    Hamilton, New Zealand
    MS-Off Ver
    2008
    Posts
    3

    Re: Validate Numeric Input

    Hi Rory
    Many thanks for your reply. There should be no leading or trailing zeroes. The values are Latitude and Longitude coordinates

    Regards
    dave

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Validate Numeric Input

    You say that these are latitude and longitude. However, latitude will never have 3 digits to the left of the decimal (ranging from 0 to +/- 90), so I am guessing that the 3 digit requirement applies only to the longitude results (unless you want to insist on the leading 0 for latitude numbers).

    Your profile says you are in New Zealand -- are you expecting all entries to be in/near New Zealand? My old wall map shows New Zealand spanning something from +155 degrees (east) to -165 degrees (west). I would probably set up my data validation to cover the expected numeric range of values (between -165 and -180 or between +155 and +180). Maybe this project considers a broader longitude range. In either case, I would identify the range of allowed values. 3 digits to the left suggests the values must be between -100 and -180 or between +100 and +180.

    I would want to understand the requirement to enter 6 digits past the decimal without allowing for trailing zeros. It seems kind of arbitrary to me to insist that a longitude cannot take on the value of, for example, +145.678910. That seems like a perfectly valid longitude to me, so I would want to understand why you would want to declare that entry invalid.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Registered User
    Join Date
    03-08-2021
    Location
    Hamilton, New Zealand
    MS-Off Ver
    2008
    Posts
    3

    Re: Validate Numeric Input

    Hi

    What I'm trying to do is populate cells with 2 sets of Lat/Long selected by user option from the 2 formats below, ensuring that they enter the correct syntax to allow me to take those coordinates and calculate the distance between them.

    deg-min-sec suffixed with N/S/E/W (e.g. 40°44′55″N, 73 59 11W), or
    decimal degrees where negative indicates west/south (e.g. 40.7486, -73.9864):

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Validate Numeric Input

    If you disallow the user to use the first entry format and only allow the decimal degree entry, it seems like the data validation need only be to constrain the entry to a value between -90 and +90 for latitude and -180 and +180 for longitude.

    If you have an algorithm/procedure that will parse the deg-min-sec-NSEW text string into a signed decimal degree value, then you could test for the same constraints after converting to decimal degree. The main challenge here is, if you don't already have a text to number algorithm/procedure in place, we need to come up with a procedure that can convert the text to a number (I wonder if regular expressions could be useful here?).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to validate an input date
    By The_Snook in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-17-2020, 10:03 AM
  2. Using regex to validate input
    By Heathy65 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2018, 06:19 AM
  3. [SOLVED] VBA code to validate numeric data
    By wilster98 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-04-2017, 02:01 PM
  4. [SOLVED] Need Code to validate multiple numeric entries
    By KiwiRickToo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-21-2015, 05:05 AM
  5. Limit input to ONLY Numeric input and not alpha characters
    By aresquare1 in forum Excel General
    Replies: 3
    Last Post: 08-25-2015, 09:38 AM
  6. [SOLVED] vba macros to force user to input numeric values for numeric values with hyphen
    By Abdur_rahman in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-28-2013, 01:05 PM
  7. Validate user input
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-31-2008, 04:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1