Excel VB Greater-than or Lesser-than Comparison Statement

Hello Experts,

I am having trouble with my if statements in a Excel VB macro - I think all the if statements are returning true and the macro is accomplishing just the last statement.

'Sub RudderCableMin()

'Rudder Cable Min Slope Intercept

Dim Rmin As Double ' Minimum Rudder Cable Tension in LBS


Rmin = Range("A9").Value


    'If 90 > Rmin < 100 Then _
        Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D18) + Sheet3!E18"
           
    If 100 > Rmin < 120 Then _
        Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D19) + Sheet3!E19"
       
    'If 120 > Rmin < 140 Then _
        Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D20) + Sheet3!E20"
       
    'If 140 > Rmin < 160 Then _
        Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D21) + Sheet3!E21"
   
'End Sub
 
Thanks
gecf343b1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zorvek (Kevin Jones)ConsultantCommented:
    If Rmin >= 90 And Rmin < 100 Then _
       Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D18) + Sheet3!E18"
         
    If Rmin >= 100 And Rmin < 120 Then _
       Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D19) + Sheet3!E19"
     
    If Rmin >= 120 And Rmin < 140 Then _
       Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D20) + Sheet3!E20"
     
    If Rmin >= 140 And Rmin < 160 Then _
       Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D21) + Sheet3!E21"

Kevin

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HiepTestCommented:
I think VB does not understand if 90 < Rmin statement
your proc should be rewrote like this:
HiepTestCommented:
here is the code
Sub RudderCableMin()

'Rudder Cable Min Slope Intercept

Dim Rmin As Double ' Minimum Rudder Cable Tension in LBS


Rmin = Range("A9").Value
    If Rmin > 90 Then
        If Rmin > 100 Then
            If Rmin > 120 Then
                If Rmin > 140 Then
                    If Rmin < 160 Then
                        Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D21) + Sheet3!E21"
                    End If
                Else
                    Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D20) + Sheet3!E20"
                End If
            Else
                Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D19) + Sheet3!E19"
            End If
        Else
            Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D18) + Sheet3!E18"
        End If
    End If
End Sub

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Chris BottomleySoftware Quality Lead EngineerCommented:
I agree with Kevin BUT I depending on teh scope of your code then perhaps you mean it as one level of construct and perhaps as a nested construct.  The following is represented as a single level construct but with elseif instead of multiple if's and the end if termination.

Chris
    If Rmin >= 90 And Rmin < 100 Then _
      Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D18) + Sheet3!E18"
        
    ElseIf Rmin >= 100 And Rmin < 120 Then _
      Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D19) + Sheet3!E19"
    
    ElseIf Rmin >= 120 And Rmin < 140 Then _
      Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D20) + Sheet3!E20"
    
    ElseIf Rmin >= 140 And Rmin < 160 Then _
      Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D21) + Sheet3!E21"
    End If

Open in new window

zorvek (Kevin Jones)ConsultantCommented:
   Select Case Rmin
        Case 90 To 99: Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D18) + Sheet3!E18"
        Case 100 To 119: Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D19) + Sheet3!E19"
        Case 120 To 139: Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D20) + Sheet3!E20"
        Case 140 To 159: Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D21) + Sheet3!E21"
        Case Else: Range("A12").Formula = "= (Sheet1!A9 * Sheet3!D21) + Sheet3!E21"
    End Select

Kevin
gecf343b1Author Commented:
Thanks - it works great
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.