+ Reply to Thread
Results 1 to 6 of 6

Restrict variable values between 0 and 1

  1. #1
    Registered User
    Join Date
    03-20-2009
    Location
    Lithuania
    MS-Off Ver
    Excel 2013
    Posts
    99

    Restrict variable values between 0 and 1

    Hi,

    I'd like to create new variable type, where the variable could be decimal and would be between 0 and 1 (like a probability). Is it possible to restrict variable between some lower and upper values?

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Restrict variable values between 0 and 1

    Why don't you use Data Validation for your cells.

    Select your cell, Select the Data Tab and then Data Validation.

    Set your validation to

    Decimal

    Min 0 Max 1
    Attached Images Attached Images
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Restrict variable values between 0 and 1

    VBA does not support that as a built-in feature (the only language I know that does this is Ada). You can build a Class to do this. Here is the class, and a test:

    Class Module Unit:
    Please Login or Register  to view this content.
    Test driver:
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-20-2009
    Location
    Lithuania
    MS-Off Ver
    Excel 2013
    Posts
    99

    Re: Restrict variable values between 0 and 1

    Thank you 6StringJazzer. I was reading about defining variables in Class module, but couldn't find a working example. Your example is great. Thank you again.
    Last edited by walduxas; 02-05-2021 at 02:24 AM.

  5. #5
    Registered User
    Join Date
    03-20-2009
    Location
    Lithuania
    MS-Off Ver
    Excel 2013
    Posts
    99

    Re: Restrict variable values between 0 and 1

    Quote Originally Posted by mehmetcik View Post
    Why don't you use Data Validation for your cells.

    Select your cell, Select the Data Tab and then Data Validation.

    Set your validation to

    Decimal

    Min 0 Max 1
    Thank's, but the question is about working with VBA, not working in Excel sheet. One of the user defined functions has the parameter, which should be limited to have values between 0 and 1.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Restrict variable values between 0 and 1

    Quote Originally Posted by walduxas View Post
    One of the user defined functions has the parameter, which should be limited to have values between 0 and 1.
    I gave a very general-purpose solution to your question. If you are specifically dealing with input to a UDF, that is an overly complicated solution. It may make more sense to simply check the value in the UDF rather than creating a class to deal with it:

    Please Login or Register  to view this content.

+ 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] Restrict to paste values only
    By duyvh0309 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-15-2020, 05:59 AM
  2. Restrict to paste values only
    By Zahid0111 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-26-2020, 11:54 AM
  3. Restrict number of numeric values in a cell
    By inoka in forum Excel Programming / VBA / Macros
    Replies: 35
    Last Post: 06-20-2018, 10:56 AM
  4. [SOLVED] restrict collection to text values only
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-24-2018, 11:07 AM
  5. Restrict Values That Can Be Entered in a Cell
    By Blokeman in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-17-2015, 09:06 PM
  6. Replies: 13
    Last Post: 08-17-2011, 02:16 PM
  7. Restrict values on columns
    By parthaemail in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-09-2007, 02:42 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