Sign in to follow this  
Followers 0
Andy2k14

If then else problem

6 posts in this topic

Hi all,

can anyone tell me why this code does not run as expected.

;Pick out last 2 characters 
Local $sString = StringRight($sText, 2) ; Retrieve 2 characters from the right of the string.

If $sString == 'RE' Then
   $OutputDir = 'C:\Folder1'
 
ElseIf $sString == 'RF' Then
   $OutputDir = 'C:\Folder2'
  
ElseIf $sString == '9J' or 'JA' or 'JB' or 'JC' or 'S1' Then
   $OutputDir = 'C:\Folder3'
  
ElseIf $sString == '6E' or '7E' or '8E' Then
   $OutputDir = 'C:\Folder4'

Else
   MsgBox($MB_SYSTEMMODAL, "", "Type Not Recognised") ;unrecognised part, exit program
   Exit
EndIf

Its supposed to work like this:

1) get last two characters from string

2) If characters match change  $OutputDir

3)If none of the characters match produce error message.

It sort of works but the message box error message never seems to trigger regardless.

thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

this is not what you think:

ElseIf $sString == '9J' or 'JA' or 'JB' or 'JC' or 'S1' Then

this does not translate to English as if string equals "9J" or string equals "JA" etc.

it translates to if string equals the result of the mathematical operation ("9J" Or "JA" etc.) - which i have no idea what that is, but most definitely not what you meant.

use Switch...Case...EndSwitch instead of If..ElseIf...Then...EndIf.

Edited by orbs

Share this post


Link to post
Share on other sites

Variable $sText is not defined.

Please post complete script.


My UDFs, Abbrevs and Snippets
If you like my post, just click the like button :) 

Share this post


Link to post
Share on other sites

it translates to if string equals the result of the mathematical operation ("9J" Or "JA" etc.) - which i have no idea what that is, but most definitely not what you meant.

 

actually, my mistake. it translates to if string equals "9J" or the boolean result of "JA" (which is always True) etc.

still, this suggestion still applies:

use Switch...Case...EndSwitch instead of If..ElseIf...Then...EndIf.

Share this post


Link to post
Share on other sites

Switch $sString
    Case 'RE'
        $OutputDir = 'C:\Folder1'
    Case 'RF'
        $OutputDir = 'C:\Folder2'
    Case '9J','JA','JB','JC','S1'
        $OutputDir = 'C:\Folder3'
    Case '6E','7E','8E'
        $OutputDir = 'C:\Folder4'
    Case Else
        MsgBox($MB_SYSTEMMODAL, "", "Type Not Recognised") ;unrecognised part, exit program
        Exit
EndSwitch

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0