PDA

View Full Version : [SOLVED] Easy If-Than code for comparison of multiple values doesn't return correct value



quetzal
05-24-2016, 06:21 AM
Hi, I just couldn't solve this issue. My B5 cell contains the number 3007998 which is the input for the computation and my code doesn't return the correct value:


If CStr(Sheets("ORDERS").Range("B5").Value) = "3007659" Or "3007664" Or "3008085" Then
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End If

...it returns the SpeedRate = 462 which is not correct and idk why..even if I remove the conversion to string by CStr.

p45cal
05-24-2016, 07:19 AM
One of:

Sub blah()
If CStr(Sheets("ORDERS").Range("B5").Value) = "3007659" Or CStr(Sheets("ORDERS").Range("B5").Value) = "3007664" Or CStr(Sheets("ORDERS").Range("B5").Value) = "3008085" Then
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End If
End Sub

Sub blah2()
Select Case CStr(Sheets("ORDERS").Range("B5").Value)
Case "3007659", "3007664", "3008085"
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Case Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End Select
End SubHowever, if cell B5 already contains a number then one of:

Sub blah3()
If Sheets("ORDERS").Range("B5").Value = 3007659 Or Sheets("ORDERS").Range("B5").Value = 3007664 Or Sheets("ORDERS").Range("B5").Value = 3008085 Then
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End If
End Sub

Sub blah4()
Select Case Sheets("ORDERS").Range("B5").Value
Case 3007659, 3007664, 3008085
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Case Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End Select
End Sub

quetzal
05-24-2016, 07:24 AM
One of:

Sub blah()
If CStr(Sheets("ORDERS").Range("B5").Value) = "3007659" Or CStr(Sheets("ORDERS").Range("B5").Value) = "3007664" Or CStr(Sheets("ORDERS").Range("B5").Value) = "3008085" Then
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End If
End Sub

Sub blah2()
Select Case CStr(Sheets("ORDERS").Range("B5").Value)
Case "3007659", "3007664", "3008085"
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Case Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End Select
End SubHowever, if cell B5 already contains a number then one of:

Sub blah3()
If Sheets("ORDERS").Range("B5").Value = 3007659 Or Sheets("ORDERS").Range("B5").Value = 3007664 Or Sheets("ORDERS").Range("B5").Value = 3008085 Then
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End If
End Sub

Sub blah4()
Select Case Sheets("ORDERS").Range("B5").Value
Case 3007659, 3007664, 3008085
SpeedRate = 462
MsgBox "Speedrate is 462pcs/h"
Case Else
SpeedRate = 625
MsgBox "Speedrate is 625pcs/h"
End Select
End Sub


thank you very much! the first one worked just fine