Using string in if Function

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
philmcmannis
Newbie
Posts: 10
Joined: Tue Jun 26, 2007 2:46 pm

Using string in if Function

Post by philmcmannis » Tue Jun 26, 2007 3:19 pm

Hi

I'm trying to use a string variable I have stored in and IF statement and I can't get it to work.

The code I'm using is

The have set the Country variable using the Getclipboard command. I know the variable is storing correctly as I've used messagemodal to pull it up and it is storing Canada

If>%Country%="Canada",Canada,Countryset
Label>Canada
//bunch of code here that I want run if Country=Canada
Label>Countryset
//Code I want if Country is not equal to

The script is only running Canada and not going to the Countryset Label

Please help!

Thanks!
________
Alaska Dispensary
Last edited by philmcmannis on Sat Feb 12, 2011 4:01 am, edited 1 time in total.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Re: Using string in if Function

Post by jpuziano » Tue Jun 26, 2007 3:43 pm

philmcmannis wrote:The script is only running Canada and not going to the Countryset Label

Please help!

Thanks!
Hi philmcmannis,

That's because in the If> statement, you are testing whether the variable Country is equal to "Canada" (including the double quotes). Just remove the double quotes and it will work as you expect. Try this code... run it once like it is and then uncomment the //Let>Country=France line and comment out the Let>Country=Canada line and run it again:

Code: Select all

Let>Country=Canada
//Let>Country=France

If>%Country%=Canada,Canada,Countryset
Label>Canada
//bunch of code here that I want run if Country=Canada
MDL>We have reached the line: Label>Canada
GoTo>end

Label>Countryset
//Code I want if Country is not equal to
MDL>We have reached the line: Label>Countryset

Label>end
Also, in the line...
  • If>%Country%=Canada,Canada,Countryset
...you don't really need the percent signs around Country, this line will work just as well:
  • If>Country=Canada,Canada,Countryset
Welcome to the forums and take care.
Last edited by jpuziano on Tue Jun 26, 2007 3:51 pm, edited 1 time in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jun 26, 2007 3:49 pm

It's both a blessing and a curse but you don't "quote" strings in macroscheduler. And you don't need the % in an If> (unless you have set the VAREXPLICIT system variable to require them).

Try:

Let>Country=Canada
If>Country=Canada,Canada,Countryset
Label>Canada
MDL>This is Canada
Goto>theend
Label>Countryset
MDL>This is Countryset
Goto>theend
Label>theend

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jun 26, 2007 3:52 pm

Lots of answers here on the forums... :wink:
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jun 26, 2007 3:52 pm

Darn this phpbb that doesn't warn you if another reply has been posted since you opened the page. SMF rules!

philmcmannis
Newbie
Posts: 10
Joined: Tue Jun 26, 2007 2:46 pm

Post by philmcmannis » Tue Jun 26, 2007 4:04 pm

Thanks for the quick response. My IF statement is working correctly which means this is actually related to what I'm trying to copy out of Excel.

What I'm trying to do is Copy a cell in Excel, Store that text as a variable and then evaluate that variable with an if command.

I've used messagemodal to confirm that Data is getting picked up, but I'm assuming there is some extra "excel stuff" being copied as well. What should I do?

Press Ctrl
Wait>0.10
Send>c
Wait>0.10
Release Ctrl
WaitClipboard>2
GetClipboard>Country
MEssageModal>%Country%

The message box returns Canada, but the if statement doesn't detect Canada.

Thoughts?
________
Mxr-01
Last edited by philmcmannis on Sat Feb 12, 2011 4:01 am, edited 1 time in total.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jun 26, 2007 4:20 pm

philmcmannis wrote:I'm assuming there is some extra "excel stuff" being copied as well. What should I do?
Yup, that's what I'd say is happening alright... Try the following to "clean up" your variable...

Code: Select all

VBEval>Replace("%val%",chr(13),""),val 
VBEval>Replace("%val%",chr(10),""),val 
VBEval>Trim("%val%"),val 

First line replaces carriage returns with nothing. Next line replaces linefeeds with nothing. Final line removes trailing and leading spaces.
The above isn't my code, just pulled this out of snippets... some other forum poster graciously shared this with us all a while ago.

Also, if you use the above code, you'll need to add the following two lines as well, right at the top of your script would be best...
  • VBSTART
    VBEND
...because, to quote the Help file entry on VBSTART... "If you wish to evaluate VBScript expressions in your scripts you will need a VBSTART/VBEND block at the start of your script even if it contains no code."

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jun 26, 2007 4:23 pm

Me_again wrote:Darn this phpbb that doesn't warn you if another reply has been posted since you opened the page. SMF rules!
Yes... its happened to me many times before as well. Sometimes I even open a separate browser window and re-check the original forum posting to check if someone else has already replied... before posting my reply.

I didn't know that SMF had a warning feature... very interesting. Someone should put in an enhancement request for this feature over at phpBB. If it came out in a future release, one day we could be enjoying that feature here too.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

philmcmannis
Newbie
Posts: 10
Joined: Tue Jun 26, 2007 2:46 pm

Post by philmcmannis » Tue Jun 26, 2007 4:42 pm

I just tried inserting the code you wrote after grabbing the text from excel.

What I put in is:
VBEval>Replace("%RecordCount%",chr(13),""),RecordCount
VBEval>Replace("%RecordCount%",chr(10),""),RecordCount
VBEval>Trim("%RecordCount%"),RecordCount


I'm getting an error that says

Microsoft VBScript compilation error :1033
Unterminated string constant
Line 23, Column 10

Any idea what's causing that?
________
BUY VOLCANO VAPORIZER
Last edited by philmcmannis on Sat Feb 12, 2011 4:01 am, edited 1 time in total.

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jun 26, 2007 5:10 pm

philmcmannis wrote:I'm getting an error that says

Microsoft VBScript compilation error :1033
Unterminated string constant
Line 23, Column 10

Any idea what's causing that?
Not sure but the following works for me without any errors... give this a try:
  • VBSTART
    VBEND

    //Ensure the following line ends with a SPACE character
    Let>Country=Canada

    //Ensure the following line ends with a SPACE character
    //Let>Country=France

    Let>RecordCount=Country

    //Remove Carriage Returns
    VBEval>Replace("%RecordCount%",chr(13),""),RecordCount

    //Remove Linefeeds
    VBEval>Replace("%RecordCount%",chr(10),""),RecordCount

    //Remove leading and trailing spaces
    VBEval>Trim("%RecordCount%"),RecordCount

    Let>Country=RecordCount

    If>Country=Canada,Canada,Countryset
    Label>Canada
    //bunch of code here that I want run if Country=Canada
    MDL>We have reached the line: Label>Canada
    GoTo>end

    Label>Countryset
    //Code I want if Country is not equal to
    MDL>We have reached the line: Label>Countryset

    Label>end
Note that in the following two lines...
  • //Ensure the following line ends with a SPACE character
    Let>Country=Canada_

    //Ensure the following line ends with a SPACE character
    //Let>Country=France_
...there is actually a SPACE character at the end, where the underscore _ chars are shown above.... as we are testing the ability of this code to clean up a trailing space.

Let us know how this works for you...
Last edited by jpuziano on Tue Jun 26, 2007 5:58 pm, edited 1 time in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jun 26, 2007 5:51 pm

Why not just use StringReplace?

StringReplace>Country,%CRLF%,,Country

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jun 26, 2007 6:06 pm

Me_again wrote:Why not just use StringReplace?

StringReplace>Country,%CRLF%,,Country
Yes, that will work fine for %CR% and %LF% (done separately on two different lines as you might have data that has just one or the other) but if you try to use StringReplace> to trim leading and trailing spaces, it will remove spaces *from within* the string as well. New Zealand would be transformed into NewZealand and that's not what we want... so I prefer to just use the VBScript method for all three.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Jun 26, 2007 6:46 pm

Well, yes, you'll note I didn't suggest using it for space :wink: My experience is that a cell copied from excel has a CRLF at the end.

philmcmannis
Newbie
Posts: 10
Joined: Tue Jun 26, 2007 2:46 pm

Post by philmcmannis » Tue Jun 26, 2007 7:15 pm

I've got it going now. Used to stringreplace as spaces don't have an effect on what I'm trying to do.

Thanks!!
________
Robert lutz

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts