+ Reply to Thread
Results 1 to 13 of 13

Validating a cell contains an integer

  1. #1
    Registered User
    Join Date
    10-24-2014
    Location
    Gloucestershire, England
    MS-Off Ver
    Excel 2007
    Posts
    11

    Validating a cell contains an integer

    I have written some code to validate that a user input field contains a whole number;

    If VarType(Range("Quantity1")) = vbInteger Then

    Else: MsgBox "Quantity must be a whole number, to rectify this please enter a whole number in the quantity field.", vbCritical, "Operator Response Required"
    Range("Quantity1").ClearContents
    Range("Quantity1").Select
    Exit Sub
    End If


    This code returns an error message regardless of Quantity1 having an integer or not.
    Can anyone help with this?

    Quantity1 is the name of the cell to be validated.

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Validating a cell contains an integer

    Is "Quantity1" a cell on a spreadsheet?
    VarType is supposed to be used with a variable.
    You would have to create a variable, let's say x.
    x = Range("Quantity1").Value
    If VarType(x)......

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Validating a cell contains an integer

    Try vbdouble(not tested)
    Please Login or Register  to view this content.

  4. #4
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Validating a cell contains an integer

    Quote Originally Posted by jindon View Post
    Try vbdouble(not tested)
    Please Login or Register  to view this content.
    How does that test for an integer?

    Here's a thread you were involved in back in 2004.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Validating a cell contains an integer

    That's different issue.

    Term Integer and vbInteger of VarType are different.

    see
    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Validating a cell contains an integer

    Quote Originally Posted by cultofcargo View Post
    I have written some code to validate that a user input field contains a whole number;
    Perhaps, OP wants to do like
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-24-2014
    Location
    Gloucestershire, England
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Validating a cell contains an integer

    Quote Originally Posted by skywriter View Post
    Is "Quantity1" a cell on a spreadsheet?
    VarType is supposed to be used with a variable.
    You would have to create a variable, let's say x.
    x = Range("Quantity1").Value
    If VarType(x)......
    Yes Quantity1 is a cell

    x = Range("Quantity1").Value
    If VarType(x) = vbInteger Then
    Else
    MsgBox "Quantity must be a whole number, to rectify this please enter a whole number in the quantity field.", vbCritical, "Operator Response Required"
    Range("Quantity1").ClearContents
    Range("Quantity1").Select
    Exit Sub
    End If


    With this written i am still getting the error message no matter what is in the cell.

    And storing x (quantity1) using Dim will return messages based on what i store it as in the Dim statement (string, boolean, integer)

  8. #8
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Validating a cell contains an integer

    Look at Jindon's post #6 and/or the link I posted in post #4.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Validating a cell contains an integer

    All numbers in Excel are stored as Doubles. If you want to test that one is a whole number, then

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Validating a cell contains an integer

    Quote Originally Posted by shg View Post
    All numbers in Excel are stored as Doubles. If you want to test that one is a whole number, then
    Please Login or Register  to view this content.
    Right: this isn't rocket science. Minor typo, corrected in red above (missing parenthesis). [1]

    We might also point out that VBA Mod (jindon's Mod 1 expression) does not work for this purpose. As the help page tells us: VBA Mod works only with integer values. "If either number is a floating-point number, it is first rounded to an integer".


    -----
    [1] Personally, I would use the following; presumably more efficient.
    Please Login or Register  to view this content.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Validating a cell contains an integer

    OOps, I should have tested...
    Please Login or Register  to view this content.
    Last edited by jindon; 11-22-2015 at 07:45 PM.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Validating a cell contains an integer

    Duplicate.

  13. #13
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: Validating a cell contains an integer

    Read the whole thread. The confusion is in what does the OP want.
    The OP titles the thread with "integer", mentions whole number, but then in the first post he's using vartype vbinteger, so is he checking for -32,768 to 32,767 and he doesn't want anything outside of the VBA integer, or any integer?

    That's what I've been wondering ever since I saw vbinteger.

+ 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] Validating user name in cell
    By momoknz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2013, 11:25 PM
  2. Replies: 2
    Last Post: 06-30-2013, 03:30 PM
  3. Validating a cell value
    By tonyromero in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-28-2013, 04:50 AM
  4. Replies: 3
    Last Post: 10-27-2011, 05:20 PM
  5. Validating a cell
    By m.cain in forum Excel General
    Replies: 5
    Last Post: 07-07-2011, 03:51 PM
  6. Count the number of occurrences of an integer withing a larger integer
    By nnktran in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2010, 01:04 PM
  7. Validating a Cell
    By leelu_uma in forum Excel General
    Replies: 2
    Last Post: 11-05-2008, 06:47 AM
  8. [SOLVED] Validating certain cell
    By Kasey in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-17-2006, 10:50 AM

Tags for this Thread

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