Sign in to follow this  
Followers 0
Snow1

If statement

9 posts in this topic

I have searched everywhere for a clue on why this doesn't work. Can anyone help me? I'm copying date from one application to Excel. I need to make sure that certain cells have the same exact values.

I have the following as an example:

"If ($A1 <> $A4) then Exit" . If the cells don't match stop the script!

This and variations of the same simply do not work and I just don't know enough about Autoit!

Thanks in advance.

Share this post


Link to post
Share on other sites



How did you put the data from the cells into the variables $A1 and $A4. Some cell format types are interpreted and the .text of the cell is not the same as the .value.

The only line of code you showed is fine, but it matters what is in those variables and how it got there.

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Here is what I have. It's looped but thats not used while I try to find how to use "If".

This sends data from A4 to our software application, then I copy what was entered, paste that back to A1 then compare A1 to A4.

This is repeated many time with different cells so if I can get the first to work (A1<>A4) I'll be able to do the others.

Everything works fine, I just can't do the comparison.

Send("JV Miscellaneous Issue2") ;Selects this form

Send("!{o}") ;Accepts Ok to open form

Sleep(1000)

;;Do ;repeats the script till "Until" = ""

Sleep(1000)

$sCellValue = _ExcelReadCell($oExcel, "A4") ;Copies Excel Part Number

Sleep(1000)

;;If $sCellValue = 0 Then ExitLoop ;Stops script if blank cell

;;Sleep(1000)

Send($sCellValue) ;Send Part Number from A4

Sleep(1000)

Send("+{LEFT 7}")

Send("^{c}")

_ExcelWriteCell($oExcel, $sCellValue, "A1")

Send("{TAB}") ;Tab to quantity

Sleep(1000)

$sCellValue = _ExcelReadCell($oExcel, "D4") ;Copies Excel quantity

Sleep(1000)

send($sCellValue) ;Send quantity

Sleep(1000)

Send("^{c}")

_ExcelWriteCell($oExcel, $sCellValue, "D1")

If "A1"<>"A4" then Exit

etc

Share this post


Link to post
Share on other sites

If "A1"<>"A4" then Exit

etc

You'll have to make sure the formats are correct for what effer Excel wants to do with it.

On way is to force the same format like:

If Number($A1) <> Number($S4) then exit

Or

If String($A1) <> String($S4) then exit


Share this post


Link to post
Share on other sites

If "A1"<>"A4" then Exit

etc

Will exit every time since literal "A1" is not equal to "A4"

use: If $A1 <> $A4 then exit

instead


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

Thank you both for looking at this, but....

"If Numbers($A1) <> Numbers($A4) then exit"

or

"If String($A1) <> String($A4) then exit"

or

"If $A1 <> $A4 then exit"

or

"If ($A1 <> $A4) then Exit"

I have A1 with a value of 2 and A4 with a value of 722041

None of the above work. I purposely changed A1 to a different number so it isn't a match but the script just continues to run.

I know I'm missing something but don't have any idea what!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I'm not seeing any purpose for all the Sends() and Sleeps()...

You can read and write to cells without all that fuss.

Anyway, you're wanting to compare the values contained within two cells?

You need to execute ExcelReadCell() and store the results in variables.

You've already done this with one cell giving you $sCellValue.

You need to read the cell value of the other into something like $sCellValue2, and then compare

"If $sCellValue <> $sCellvalue2 Then"

or probably less efficient cpu-wise but this would work as well

"If _ExcelReadCell($oExcel, "A1") <> _ExcelReadCell($oExcel, "A4") Then"

Edit:

Since you've just moved the value of A4 to A1, I'm not seeing how they can ever differ, but have to assume you're not telling us all the details and that a period of time elapses somewhere wherein human intervention can take place resulting in cell values being manually modified.

PS - Adding something like

$A1 = _ExcelReadCell($oExcel, "A1")

$A4 = _ExcelReadCell($oExcel, "A4")

Would get your earlier attempts using "If $A1 <> $A4" working, and might make for more meaningful variable names than $sCellValue, etc.

Edited by Spiff59

Share this post


Link to post
Share on other sites

If you would post the running code in AutoIt tags, it would be easier to help you.

Some edited snippets aren't helpfull.


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

#9 ·  Posted (edited)

Thank you both for looking at this, but....

"If Numbers($A1) <> Numbers($A4) then exit"

or

"If String($A1) <> String($A4) then exit"

or

"If $A1 <> $A4 then exit"

or

"If ($A1 <> $A4) then Exit"

I have A1 with a value of 2 and A4 with a value of 722041

None of the above work. I purposely changed A1 to a different number so it isn't a match but the script just continues to run.

I know I'm missing something but don't have any idea what!

Putting 2 in cell A1 and 722041 in cell A4 result in the message not Equil.

Putting 722041 in both cells result in a Equil message.

Try:

#include <Excel.au3>
$oExcel = _ExcelBookOpen(@DesktopDir & "\test.xls", 1)
$A1 = _ExcelReadCell($oExcel, "A1")
MsgBox(0, "", "The Cell Value is: " & @CRLF & $A1, 2)
$A4 = _ExcelReadCell($oExcel, "A4")
MsgBox(0, '', $A4)
If $A1 <> $A4 Then
    MsgBox(0, "Not Equil", "Yes", 1)
Else
    MsgBox(0, "Is Equil", "Yes", 1)
EndIf
_ExcelBookClose($oExcel)
Edited by JoHanatCent

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