Consulting

Results 1 to 7 of 7

Thread: Between two numbers

  1. #1
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location

    Between two numbers

    Hi to all . I want to write a statement with an " if " between two numbers , how can it be done? eg If 150 between 100 and 200 then ... "do something" end if
    Regards
    Last edited by ksilenio; 11-12-2019 at 02:14 PM.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,640
    In A1: 30
    In A2:200
    In A3:100

    PHP Code:
    =INT(A1/(A2-A3))=

  3. #3
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location
    Hi thank you for your answer but i don't understant , how can i use this in the VBA code , what i want is how to syntax the "if" statement

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    If number1 >100 and number1 < 200 then
    '...do whatever you want done
    End If
    or on one line:
    If number1 >100 and number1 < 200 then '...do something
    where number1 is a variable containing your number (150 in your example).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,640
    PHP Code:
    Sub M_snb()
       
    99
       y 
    200
       Z 
    100
       
    If Int((Z) / (Z)) = 0 Then MsgBox x " lies between " " and " y
    End Sub 
    Last edited by snb; 11-13-2019 at 03:31 AM.

  6. #6
    VBAX Regular
    Joined
    Aug 2019
    Posts
    54
    Location
    OK Thank you

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Personal style

    1. I find it's better if I use parentheses to group each part of the AND

    2. I like to arrange the conditionals is what to me is a logical order based on the way I think

    L <= N <= H

    3. I don't see any reason to squeeze everything onto one line since I find it can make things harder to read

    Just seems to make it easier for me


    Sub test()
        Dim n As Long, L As Long, H As Long
    
    
        n = 150
        L = 100
        H = 200
        
        If (L <= n) And (n < H) Then
            MsgBox n & " is between " & L & " and " & H
        Else
            MsgBox "nope"
        End If
    
    
    End Sub

    If you had to do a lot, you could make a boolean function. It's simple, but might make it easier to follow the logic. Probably costs a few insignificant CPU cycles

    Sub test2()
        Dim N As Long, L As Long, H As Long
    
    
        N = 150
        L = 100
        H = 200
        
        If Between(L, N, H) Then
            'Do Something
        End If
    
    
    End Sub
    
    
    
    
    Function Between(L As Variant, N As Variant, H As Variant) As Boolean
        Between = (L <= N) And (N < H)
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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