×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual basic "if statements"
2

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
         

RE: Visual basic "if statements"

2
Is this all of the pertinent code, because it is missing an End If statement?

RE: Visual basic "if statements"

(OP)
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.

RE: Visual basic "if statements"

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.

RE: Visual basic "if statements"

(OP)
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

RE: Visual basic "if statements"

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

RE: Visual basic "if statements"

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  ' 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"

(OP)
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?

RE: Visual basic "if statements"

See previous post.

RE: Visual basic "if statements"

(OP)
CajunCenturion,
Your input is very helpful.  Thanks for your help.

Structural01

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close