+ Reply to Thread
Results 1 to 10 of 10

How to check if number is divisible by 6

Hybrid View

  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...

                                pquant = cells(arrow, acol)
                                Select Case pquant
                                    Case 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, _
                                    96, 102, 108, 114, 120, 126, 132, 138, 144, 150, 156, 162, 168, _
                                    174, 180, 186, 192, 198, 204, 210, 216, 222, 228, 234, 240, 246, _
                                    252, 258, 264, 270, 276, 282, 288, 294, 300
                                        'do nothing
                                    Case Else
                                        'code to correct here....
                                End Select
    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.
        pquant = Cells(arrow, acol)
        If pquant <= 300 And pquant Mod 6 = 0 Then
            'do nothing
        Else
            'code to correct here....
        End If
    多么想要告诉你 我好喜欢你

  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:

    function function_that_checks(item as long) as boolean
    If item <= 300 And item Mod 6 = 0 Then
            function_that_checks = True
        Else
            function_that_checks = False
        End If
    end function
    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.

    Function function_that_checks(item as Long) as Boolean
        If item <= 300 And item Mod 6 = 0 Then  function_that_checks = True
    End Function
    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"
    Sub test()
    Dim pquant As long
    
    pquant = 1
    Do
    Call checks
    pquant = pquant + 1
    MsgBox (pquant)
    Loop Until pquant = 10
    endsub
    
    
    Function checks(pquant As Long) As Boolean
        If pquant = 5 Then
            checks = True
    End Function

    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...
    Function checks(pquant As Long) As Boolean
        If pquant = 5 Then checks = True
    End Function

  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....
    Sub test()
    Dim pquant As Long
    Dim tf As Boolean
    pquant = 1
    Do
    tf = checks(pquant)
    MsgBox (pquant & " " & tf)
    pquant = pquant + 1
    Loop Until pquant = 10
    End Sub
    
    
    Function checks(pquant As Long) As Boolean
        If pquant = 5 Then
            checks = True
        End If
    End Function
    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

    Function checks(pquant As Long) As Boolean
        checks = (pquant = 5)
    End Function
    _
    ...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