Click to See Complete Forum and Search --> : if and elseif


midnightservice
September 14th, 2001, 11:53 PM
okay here's my code,

Sub SubComputeArea_Click()
Dim sum As Long
Dim sum1 As Long
Dim sum2 As Long
sum1 = CLng(Text1)
sum2 = CLng(Text2)
sum = (sum1 + sum2)
Text3 = sum
If Text3 <= 61 Then
Text4 = 107.16 * 0.4699
Text5 = 115.64 * 0.4699
Text6 = 107.16 * 0.62
Text7 = 115.64 * 0.62
End If

If Text3 >= 61 Then
Text4 = 124.72 * 0.4699
Text5 = 134.6 * 0.4699
Text6 = 124.72 * 0.62
Text7 = 134.6 * 0.62
End If
End Sub

okay the prob is i have about 7 more >=#'s to put in, how can this be done without making a total mess of this code...please help

thanx in advance

midnightservice

Cakkie
September 15th, 2001, 06:21 AM
Well, you could use If Then ElseIf as you suggested, or you could use a SLECT CASE

If Text3 <= 61 then
Text4 = 107.16 * 0.4699
Text5 = 115.64 * 0.4699
Text6 = 107.16 * 0.62
Text7 = 115.64 * 0.62
ElseIf Text3 >= 61 then
Text4 = 124.72 * 0.4699
Text5 = 134.6 * 0.4699
Text6 = 124.72 * 0.62
Text7 = 134.6 * 0.62
End If

Select Case Text3
Case is <= 61
Text4 = 107.16 * 0.4699
Text5 = 115.64 * 0.4699
Text6 = 107.16 * 0.62
Text7 = 115.64 * 0.62
Case is >= 61
Text4 = 124.72 * 0.4699
Text5 = 134.6 * 0.4699
Text6 = 124.72 * 0.62
Text7 = 134.6 * 0.62
End Select




Tom Cannaerts
slisse@planetinternet.be

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook

dcaillouet
September 15th, 2001, 10:53 AM
Depending on what the other 7 IFs look like, a variation might be something like:

Select Case Text3
Case is <= 61
Value1 = 107.16
Value2 = 115.64
Case is >= 61
Value1 = 124.72
Value2 = 134.6
End Select

Text4 = Value1 * 0.4699
Text5 = Value2 * 0.4699
Text6 = Value1 * 0.62
Text7 = Value2 * 0.62

midnightservice
September 15th, 2001, 03:20 PM
okay works up to a point....here is what i got

Sub SubComputeArea_Click()
Dim sum As Long
Dim sum1 As Long
Dim sum2 As Long
sum1 = CLng(Text1)
sum2 = CLng(Text2)
sum = (sum1 + sum2)
Text3 = sum
Select Case Text3
Case 0 To 61
Value1 = 107.16
Value2 = 115.64
Case 62 To 72
Value1 = 124.72
Value2 = 134.6
Case 73 To 81
Value1 = 142.29
Value2 = 153.6
Case 82 To 91
Value1 = 159.76
Value2 = 172.52
Case 92 To 101
Value1 = 177.42
Value2 = 191.48
Case 102 To 111
Value1 = 194.99
Value2 = 210.43
Case 112 To 121
Value1 = 212.56
Value2 = 229.39
Case 122 To 131
Value1 = 230.12
Value2 = 248.35
End Select
Text4 = Value1 * 0.4699
Text5 = Value2 * 0.4699
Text6 = Value1 * 0.62
Text7 = Value2 * 0.62
End Sub


what is wrong...evrything from case 91 and up returns either a 0 value or a value from the begining of the case....any ideas

midnightservce

midnightservice
September 15th, 2001, 03:41 PM
okay i got it to work but any case number 100 returns the same value as case 0 to 61....why is this.....

midnightserive

dcaillouet
September 15th, 2001, 05:13 PM
I'm not at a machine where I can test your code, but if you are getting incorrect output you should be able to test it yourself. You could put a breakpoint at the beginning of the Case statement and step through your code a line at a time and see where it was going wrong.

Even if I "know" a particular condition will "never" happen, I always put a "Case Else" clause at the end of my case statement with a MsgBox saying that the values were outside the range of the statement (less than 0, greater than 131).