# 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
###### Who is Participating?
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.

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
0

Experts Exchange Solution brought to you by

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

Commented:
I think VB does not understand if 90 < Rmin statement
your proc should be rewrote like this:
0
Commented:
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
``````
0
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
``````
0
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
0
Author Commented:
Thanks - it works great
0
###### 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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.