Consulting

Results 1 to 3 of 3

Thread: Easy If-Than code for comparison of multiple values doesn't return correct value

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    22
    Location

    Easy If-Than code for comparison of multiple values doesn't return correct value

    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.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    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 Sub
    However, 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
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    May 2016
    Posts
    22
    Location
    Quote Originally Posted by p45cal View Post
    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 Sub
    However, 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •