+ Reply to Thread
Results 1 to 10 of 10

How to check if number is divisible by 6

  1. #1
    Registered User
    Join Date
    09-18-2014
    Location
    Dillsburg PA
    MS-Off Ver
    2013
    Posts
    18

    Question How to check if number is divisible by 6

    OK, so I need to know in several spots in my code that a number is both divisible by 6 and is less than 50*6... (300). I have been doing it by using the following code...

    Please Login or Register  to view this content.
    I know the better way to do it is to call a function that returns a t/f Boolean... but I've never used functions before and having trouble getting it to work.

    I'm self taught in VBA with over 10 years of experience, and I need to learn how to start making code more efficient..... thanks!

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: new to using "functions"

    Are you trying to change the condition? Here is what it looks with an If statement.
    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

  3. #3
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How to check if number is divisible by 6

    You can use functions to return values. Millz example is a good way of simplifying your code without going to a function, but if you were to, then you could use a boolean variable in your main sub, and then use it to call the function. For example you use a variable "test" in your main routine, and it is dimmed as a boolean.... then you have a function that does the analysis, and returns a boolean value:

    in main sub: test = function_that_checks(value_to_check)
    then the function looks something like:

    Please Login or Register  to view this content.
    sorry it is not a very elaborate example, but is hopefully enough to illustrate the point?
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to check if number is divisible by 6

    You can take this one step further. Since the return value of a Function is initialized to a default value, you can skip the "Else" portion. The default value for a Boolean value is false.

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    09-18-2014
    Location
    Dillsburg PA
    MS-Off Ver
    2013
    Posts
    18

    Re: How to check if number is divisible by 6

    ok... but when I call the function.... how do I pass the variable I need evaluated to the function. Does the function use the value that is currently assigned to the variable at the time it's called?

    I wrote this as a quick test, and I get a compile error "argument not optional" on the line "call checks"
    Please Login or Register  to view this content.

    help please! thanks!
    Last edited by jmilliken; 10-09-2014 at 01:31 PM. Reason: aparently, i cant type

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to check if number is divisible by 6

    Hello jmilliken,

    It is a single line If statemnt is should be like this...
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-18-2014
    Location
    Dillsburg PA
    MS-Off Ver
    2013
    Posts
    18

    Re: How to check if number is divisible by 6

    ok... I got it to work....
    Please Login or Register  to view this content.
    I think I got it from here. thanks everybody!

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to check if number is divisible by 6

    Hello jmilliken,

    You're welcome. I will mark this post as solved.

  9. #9
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to check if number is divisible by 6

    I have a pet peeve against If statements that set a boolean value

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  10. #10
    Registered User
    Join Date
    09-18-2014
    Location
    Dillsburg PA
    MS-Off Ver
    2013
    Posts
    18

    Re: How to check if number is divisible by 6

    ALWAYS LEARNING!!!! Thanks all!

+ 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. Count a reoccurring number divisible by 70 over 24 months.
    By warrenjns in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 06-08-2014, 11:45 AM
  2. selecting divisible by a number
    By bkabue in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 02-11-2014, 10:40 AM
  3. Showing if number is divisible...
    By njg2982 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-05-2007, 11:54 AM
  4. Check if Equally Divisible
    By John in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-01-2006, 09:15 AM
  5. [SOLVED] Rounding a Value to Make It Divisible by a Specified Number
    By John Nash in forum Tips and Tutorials
    Replies: 1
    Last Post: 08-29-2005, 10:53 AM

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