Jump to content

Comparing ClipGet value from Excel


Uemlue
 Share

Go to solution Solved by Subz,

Recommended Posts

Hello Guys,

 

sorry i am new in Autoit and i am trying to use comparing  two string. The frist sring i am copying a string from excel ("Test"), saving into $var with ClipGet() function. So, now i want to compare ClipGet value $var with "Test". It should be "Matched! ", but it doesn`t work. Why? I checked the variable with MsgBox and it is "Test". 

Global $var

 sleep(2000)
      send("^c")
 sleep(2000)

$var = ClipGet()


Switch $var
    Case "Test"
        MsgBox(64,"","Matched !")
    Case Else
        MsgBox(64,"","Didn't match..")
EndSwitch

 

Thanks for your help...

 

Link to comment
Share on other sites

Global $var

 sleep(2000)
;      send("^c")
 ClipPut("Test")
 sleep(2000)

$var = ClipGet()


Switch $var
    Case "Test"
        MsgBox(64,"","Matched !")
    Case Else
        MsgBox(64,"","Didn't match..")
EndSwitch

gives "Matched !" so it seems you are not copying the exact string Test from Excel. Check if you are selecting/copying a space or (double) quot's with the string Test.

Link to comment
Share on other sites

  • Solution

If you're using copy to clipboard from Excel then you should use StringStripWS($var, 3)

Normally when you copy from a cell in Excel it appends line feed at the end of the string.

StringStripWS with flag of 3, will remove any leading white space and any trailing white space.

But as @Jfish mentioned, it would be better to use Excel UDF for example:
Note the example below is using the default AutoIt Excel1.xls example file

#include <Excel.au3>

;~ Open Excel
Local $oExcel = _Excel_Open()
    If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;~ Open Workbook
Local $oWorkbook = _Excel_BookOpen($oExcel, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls")
If @error Then
    MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error opening workbook '" & @ScriptDir & "\Extras\_Excel1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
EndIf

; Read data from a single cell on the active sheet of the specified workbook
Local $sResult = _Excel_RangeRead($oWorkbook, Default, "G1")
    If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example 1", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

;~ By default the _Excel1.xls G1 equals "This is a long Story"

;~ Case insensitive comparison
Local $sCompare = "This is a long story"
If $sResult = $sCompare Then
    MsgBox(4096, "Results", "Case Insensitive Match = True")
Else
    MsgBox(4096, "Results", "Strings do not match")
EndIf

;~ Case sensitive comparison
;~ Note the == operator
Local $sCompare = "This is a long Story"
If $sResult == $sCompare Then
    MsgBox(4096, "Results", "Case sensitive match = True")
Else
    MsgBox(4096, "Results", "Case-sensitive strings do not match")
EndIf

 

Link to comment
Share on other sites

1 hour ago, Subz said:

If you're using copy to clipboard from Excel then you should use StringStripWS($var, 3)

Normally when you copy from a cell in Excel it appends line feed at the end of the string.

StringStripWS with flag of 3, will remove any leading white space and any trailing white space.

But as @Jfish mentioned, it would be better to use Excel UDF for example:
Note the example below is using the default AutoIt Excel1.xls example file

#include <Excel.au3>

;~ Open Excel
Local $oExcel = _Excel_Open()
    If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;~ Open Workbook
Local $oWorkbook = _Excel_BookOpen($oExcel, "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls")
If @error Then
    MsgBox(4096, "Excel UDF: _Excel_RangeRead Example", "Error opening workbook '" & @ScriptDir & "\Extras\_Excel1.xls'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
EndIf

; Read data from a single cell on the active sheet of the specified workbook
Local $sResult = _Excel_RangeRead($oWorkbook, Default, "G1")
    If @error Then Exit MsgBox(4096, "Excel UDF: _Excel_RangeRead Example 1", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

;~ By default the _Excel1.xls G1 equals "This is a long Story"

;~ Case insensitive comparison
Local $sCompare = "This is a long story"
If $sResult = $sCompare Then
    MsgBox(4096, "Results", "Case Insensitive Match = True")
Else
    MsgBox(4096, "Results", "Strings do not match")
EndIf

;~ Case sensitive comparison
;~ Note the == operator
Local $sCompare = "This is a long Story"
If $sResult == $sCompare Then
    MsgBox(4096, "Results", "Case sensitive match = True")
Else
    MsgBox(4096, "Results", "Case-sensitive strings do not match")
EndIf

 

First of all, thx guys. It works now. I used StringStripWS($var, 3) and now i have match. I am looking now the excel udf and trying to understand it. You awsome :) Big thx from a newbie

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...