Visual basic "if statements"
Visual basic "if statements"
(OP)
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
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





RE: Visual basic "if statements"
RE: Visual basic "if statements"
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.
RE: Visual basic "if statements"
RE: Visual basic "if statements"
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
RE: Visual basic "if statements"
CODE
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
RE: Visual basic "if statements"
CODE
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 ' Guessing Here because of the extra End if
Range("I161").Value = b
' and you get the idea for the rest
Case Else
Range("I159").Value = "N/A"
Range("I161").Value = "N/A"
End Select
End If
RE: Visual basic "if statements"
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?
RE: Visual basic "if statements"
RE: Visual basic "if statements"
Your input is very helpful. Thanks for your help.
Structural01