Results 1 to 4 of 4
  1. #1
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Atlanta, GA
    Posts
    220
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Describing values in 'If' statements (VBA Excel 2000)

    I created the following "If" statement to do something if a value either = 0 or does not = 0. I would like to change this to say "If the value is between -2 and + 2, then do something. What can I substitue in my expression?
    Thanks.
    If Range ("Prncode").Value = 0 Then
    Print2
    ElseIf Range("Prncode").Value <> 0 Then
    Print3

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Describing values in 'If' statements (VBA Excel 2000)

    You could use Select Case instead of If/Else. Example:

    <code>Sub TestSub()</code>
    <code></code>
    <code> Dim n As Long</code>
    <code> For n = -10 To 10</code>
    <code> Select Case n</code>
    <code> Case -2 To 2</code>
    <code> Debug.Print n, "True"</code>
    <code> Case Else</code>
    <code> Debug.Print n, "False"</code>
    <code> End Select</code>
    <code> Next n</code>
    <code></code>
    <code>End Sub</code>

    Test results:

    <code>-10 False</code>
    <code>-9 False</code>
    <code>-8 False</code>
    <code>-7 False</code>
    <code>-6 False</code>
    <code>-5 False</code>
    <code>-4 False</code>
    <code>-3 False</code>
    <code>-2 True</code>
    <code>-1 True</code>
    <code> 0 True</code>
    <code> 1 True</code>
    <code> 2 True</code>
    <code> 3 False</code>
    <code> 4 False</code>
    <code> 5 False</code>
    <code> 6 False</code>
    <code> 7 False</code>
    <code> 8 False</code>
    <code> 9 False</code>
    <code> 10 False</code>

    You should be able to adapt this for your project.

    HTH

  3. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Describing values in 'If' statements (VBA Exce

    if you only need to execute the same code if the value falls between these ranges, you could do the following

    If Range ("Prncode").Value >=-2 and Range ("Prncode").Value <=2 then
    'do something
    end if

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Describing values in 'If' statements (VBA Excel 2000)

    If Pincode is always an integer then Select Case is probably the way to go. If it could have a value such as -1.7 then you will need to use a syntax such as <!profile=pieter>pieter<!/profile> has suggested in <post#=389335>post 389335</post#>

    StuartR

Posting Permissions

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