Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Visual basic "if statements" 2

Status
Not open for further replies.

structural01

Structural
Jul 15, 2004
51
Hello,

I wrote the following "if statements" in visual basic and referencing them to the same cell in Excel. However, the statement meeting the right condition is being over ride by other statement not meeting the condition I specified. Can someone help? Thanks in advance.

If ((vI157) <= 0.25) And vN149 = 10 Then
c = -0.7
Range("I159").Value = c

b = -0.18
Range("I161").Value = b

End If

If ((Range("I152").Value / Range("I155").Value) <= 0.25) And vN149 = 15 Then

c = -0.5

Range("I159").Value = c

If ((vI157) <= 0.25) And vN149 = 15 Then

b = "N/A"

Range("I161").Value = "N/A"


End If

 
Replies continue below

Recommended for you

Is this all of the pertinent code, because it is missing an End If statement?
 
Cajuncenturion,
I am only showing an example of the code. Is it correct to reference many "if statements" to one cell? or do have to have many subs so they don't overide each other?
Thanks for your respond.
 
It is only a matter of the logic behind the if statements. As you code is shown, since it's only a sample, it hard to find any logic flaws, should they exist.
 
CajunCenturion,
Below is part of my code. I am trying to get the result "b" to show up in cell I161. But, other if statement with the incorrect condition is over riding the correct if statement. Thanks for your respond.

'public variables
Dim vG52 As Double
Dim vG55 As Double
Dim vG57 As Double
Dim vd73 As Double


Dim vN149 As Double
Dim vI157 As Double
Dim vI159 As Double
Dim vI161 As Double

Dim vG49 As Double
Dim vG59 As Double
Dim rr As Double

Dim c As Double
Dim cp1 As Double
Dim cp1a As Double
Dim cp2 As Double
Dim cp2a As Double
Dim b As Double
Dim d As Double

Sub calculate()


'------------------------------------
'-----------------------------------------------

vI157 = Range("I157").Value
vN149 = Range("N149").Value

'------------


If ((vI157) <= 0.25) And (vN149 > 10) And (vN149 < 15) Then

c = -(((10 - vN149) * (-0.7 + 0.5)) / (10 - 15)) - 0.7


Range("I159").Value = c

b = -(((10 - vN149) * (-0.18 - 0#)) / (10 - 15)) - 0.18


Range("I161").Value = b
Else

Range("I159").Value = "N/A"

Range("I161").Value = "N/A"

End If


If ((vI157) <= 0.25) And vN149 = 10 Then
c = -0.7

Range("I159").Value = c

b = -0.18

Range("I161").Value = b

End If

If ((vI157)) <= 0.25) And vN149 = 15 Then

c = -0.5

Range("I159").Value = c
End If


b = 0
Range("I161").Value = b



End If

'-----------------------------------------------------


If ((vI157) <= 0.25) And (vN149 > 15) And (vN149 < 20 Then

c = -(((15 - vN149) * (-0.5 + 0.3)) / (15 - 20)) - 0.5

Range("I159").Value = c

b = -(((15 - vN149) * (0 - 0.2)) / (10 - 15)) - 0

Range("I161").Value = b


End If


If ((vI157) <= 0.25) And vN149 = 20 Then
c = -0.3

Range("I159").Value = c

b = 0.2

Range("I161").Value = b


End If

'----------------------------------------

If ((vI157) <= 0.25) And (vN149 > 20) And (vN149 < 25) Then

c = -(((20 - vN149) * (-0.3 + 0.2)) / (20 - 25)) - 0.3


Range("I159").Value = c

b = -(((20 - vN149) * (0.2 - 0.3)) / (10 - 15)) + 0.2


Range("I161").Value = b

End If


If ((vI157) <= 0.25) And vN149 = 25 Then

c = -0.2

Range("I159").Value = c

b = 0.3

Range("I161").Value = b
End If

'----------------------------------------------

If ((vI157) <= 0.25) And (vN149 > 25) And (vN149 < 30) Then

c = -(((25 - vN149) * (-0.2 + 0)) / (25 - 30)) - 0.2


Range("I159").Value = c

b = -(((25 - vN149) * (0.3 - 0.3)) / (25 - 30)) + 0.3


Range("I161").Value = b


End If


If ((vI157) <= 0.25) And vN149 = 30 Then
c = -0.2


Range("I159").Value = c

b = 0.3

Range("I161").Value = b

End If

end sub
 
In the following section of code, both If statements have unbalanced parenthesis, and there is an extra End If, which could be quite critical considering the two statements immediately preceeding it.
Code:
If ((vI157)) <= 0.25) And vN149 = 15 Then
   c = -0.5
   Range("I159").Value = c
End If

b = 0
Range("I161").Value = b

End If

'-----------------------------------------------------

If ((vI157) <= 0.25) And (vN149 > 15) And (vN149 < 20 Then
 
Given (if I understand correctly) that nothing will happen unless vI157 <= 0.25, and the rest is based on the value of vN149, I would only check the value of vI157 one time, and then use a Case statement to differentiate between the values of vN149, something like the following:
Code:
If ((vI157) <= 0.25) Then
   Select Case vN149
      Case 10
         c = -0.7
         Range("I159").Value = c
         b = -0.18
         Range("I161").Value = b
      Case 11 To 14
         c = -(((10 - vN149) * (-0.7 + 0.5)) / (10 - 15)) - 0.7
         Range("I159").Value = c
         b = -(((10 - vN149) * (-0.18 - 0#)) / (10 - 15)) - 0.18
         Range("I161").Value = b
      Case 15
         c = -0.5
         Range("I159").Value = c
         b = 0  [COLOR=green]' Guessing Here because of the extra End if[/color]
         Range("I161").Value = b
[COLOR=green]' and you get the idea for the rest[/color]
      Case Else
         Range("I159").Value = "N/A"
         Range("I161").Value = "N/A"
   End Select
End If
 
CajunCenturion,
Yes, you are correct. I made a mistake copying things and deleting.

However, ignoring those mistakes, do you think returning the answer to the same cell is a problem, i.e. overiding the correct if statement?
 
CajunCenturion,
Your input is very helpful. Thanks for your help.

Structural01
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor