Jump to content
Sign in to follow this  
_leo_

Excel RangeFind is not working

Recommended Posts

Hey there! 😃

I am having a problem with the _Excel_RangeFind. I am trying to search for a value in a particular cell range. The script copies the value from the internet. Copying and saving as a variable is working fine, but as soon as it should find the value in excel, nothing happens. ( I am not getting an error)

#include <Excel.au3>
   
   Func Excel()
    Send("{CTRLDOWN}")
    Send("{c}")
    Send("{CTRLUP}")
    Local $sName = ClipGet() ;Text
    Local $sShortName = StringTrimRight ( $sName, 1) ;delete one letter
    Local $bOpenWorkBook = False, $oExcel = _Excel_Open()
    Local $sFilePath = "C:\Users\Acer\OneDrive\xyz.xlsx"
    Local $oWorkbook
    $oWorkbook = _Excel_BookAttach($sFilePath)
    If @error Then
      $oWorkbook = _Excel_BookOpen($oExcel, $sFilePath)
      $bOpenWorkBook = True
    EndIf
    sleep(15000)
    Send("{LWINDown}") 
    Send ("{up}") ;maximize window
    Send("{LWINup}") 
    sleep(1000)
    _Excel_RangeFind ($oWorkbook, $sShortName, "A3:A56")
   EndFunc

Is anyone familiar with this problem or am I just missing some basic stuff? 

Thanks for help!

Share this post


Link to post
Share on other sites

_Excel_RangeFind returns either a 2D Array of the results or error (see help file for return values).  Use _ArrayDisplay (requires #include <Array.au3> to show you the results.  Also try to use ControlSend rather than Send, Send can have be unpredictable, whereas ControlSend is targeted and less likely to have any issues.

Share this post


Link to post
Share on other sites

To make your script more robust, you can use excel method instead of using Send to maximize the window like this :

Const $xlMaximized = -4137
$oExcel.WindowState = $xlMaximized

 

Share this post


Link to post
Share on other sites
35 minutes ago, Nine said:

To make your script more robust, you can use excel method instead of using Send to maximize the window like this :

Const $xlMaximized = -4137
$oExcel.WindowState = $xlMaximized

 

Thanks! I included your suggestion in my script 🙂

Share this post


Link to post
Share on other sites
7 hours ago, Subz said:

_Excel_RangeFind returns either a 2D Array of the results or error (see help file for return values).  Use _ArrayDisplay (requires #include <Array.au3> to show you the results.  Also try to use ControlSend rather than Send, Send can have be unpredictable, whereas ControlSend is targeted and less likely to have any issues.

Hey Subz

Thank you very much for helping me once again! I replaced the send() with the ControlSend() and added the Array.

Is is possible to let the RangeFind show the result by switching to it/ making it the active cell? (like ctrl+f in excel) Because I would like to RangeRead it afterwards. If it is showed in an array, I don't know how the RangeRead could work. 

Thank you again for the productive support!

Share this post


Link to post
Share on other sites

You can use this :

$oWorkBook.ActiveSheet.Range ("B2").Activate

where B2 would be replace by the result of rangefind

Edited by Nine

Share this post


Link to post
Share on other sites

What happens if you have multiple results?  Either way you would just use the array[x][2] to get the address of the cell that was found.

Edited by Subz

Share this post


Link to post
Share on other sites

I strongly suggest to not mix the Excel UDF (uses COM to interact with Excel) and GUi automation like Send, ControlSend etc.
Most (if not all) things you can do with GUI automation can be done using COM. But COM does not interfere with the user and is much easier to debug.
Just my 2 cents worth ;)
 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites
1 hour ago, Subz said:

What happens if you have multiple results?  Either way you would just use the array[x][2] to get the address of the cell that was found.

How?

In my case, there can not be multiple results since the range find is searching for numbers from A3 to A56. These cells only contain a numberation from 1 to 50. 

The problem I am facing right now is, that the script should run by itself, without me having to click away the _ArrayDisplay window. Is the rangefind executed and the therefore the array safed, even if I don't use _ArrayDisplay?

What I am trying right now is transforming the RangeFind array result (see screenshot) into a variable using the  _Arraytostring. (does that work like that?) I have problems creating a string only of "col 2" and "row 0" value.

local $sSuchergebnis = _Excel_RangeFind ($oWorkbook, $sNummername, "A3:A56")
   sleep(1000)
   local $sfinal = _Arraytostring($sSuchergebnis, 0, 0, 2, 2)

and then I would use the function Nine sugguested:

$oWorkBook.ActiveSheet.Range ($sfinal).Activate

to activite the cell.

Appreciate your support!

arraydisplay.png

Share this post


Link to post
Share on other sites
local $aSuchergebnis = _Excel_RangeFind ($oWorkbook, $sNummername, "A3:A56")
If @error Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error finding value")
$oWorkBook.ActiveSheet.Range ($aSuchergebnis[0][2]).Activate

Try this

Share this post


Link to post
Share on other sites

@water I assumed he was copying from a third-party product, since the copy occurred prior to Excel being opened.

@_leo_ Just use $sFinal[0][2] to get the result

local $aResult = _Excel_RangeFind ($oWorkbook, $sNummername, "A3:A56")
   sleep(1000)
   $oWorkBook.ActiveSheet.Range ($aResult[0][2]).Activate

 

Share this post


Link to post
Share on other sites

But I think he tries to automate the GUI again by activating the found cell. Using _Excel_RangeCopyPaste is the way I'd suggest.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-12-15 - Version 1.6.3.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

You guys are awesome! Thank you for giving this crazy support! 

@water Thank you for the spark of wisdom :) The next line in the script is with the RangeRead (not with ctrl c). 

 

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By AnonymousX
      Hello,
      I'm struggling and not sure what I'm missing with how to use the _Excel_RangeRead function.
      Based on: https://www.autoitscript.com/autoit3/docs/libfunctions/_Excel_RangeRead.htm
      The second parameter should allow me to select different sheet names, however I can't seem to get it to work.
      I was thinking something like this (see below) would be all I need to get an array for the sheet named as "test sheet". 
      $array = _Excel_RangeRead($oWorkbook, "test sheet")  
      Can someone please help tell me what I'm missing here?
       
      Thank you,
    • By goku200
      I'm having some issues with writing to column C when an element is found. It works on C2 but it does not continue to C3, C4, C5, etc..... I'm wanting to write "test" if the element //input[@id='username'] is found  $someUser = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='username']"). I have attached my HTML and Excel file along with my AutoIt code below:
      #Include "wd_core.au3" #Include "wd_helper.au3" #Include "wd_core.au3" #Include "File.au3" #Include "Array.au3" #Include "Excel.au3" Local $sDesiredCapabilities, $sSession _WD_Startup() $Ssession = _WD_CreateSession($sDesiredCapabilities) _WD_Navigate($sSession, "https://127.0.0.1/test.html") _WD_LoadWait($sSession) Local $oExcel = _Excel_Open() Local $oWorkbook = _Excel_BookOpen($oExcel, "C:\Users\<Username>\Downloads\test.xlsx") Local $aArrayTest1 = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A")) Local $aArrayTest2 = _Excel_RangeRead($oWorkbook, 1, $oWorkbook.ActiveSheet.Usedrange.Columns("B:B")) For $i = 0 To UBound($aArrayTest1) - 1 _WD_Navigate($Ssession, $aArrayTest1[$i]) _WD_LoadWait($sSession) $someUser = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='username']") _WD_SetElementValue($sSession, $someUser, $aArrayTest2[$i]) Local $sElement = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@type='submit'][@value='Submit']") _WD_ElementAction($sSession, $sElement, 'click') _WD_LoadWait($sSession) Sleep(5000) If $someUser Then Local $aArray2D[2] = ["test"] _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $aArray2D, "C2") EndIf Next Func SetupChrome() _WD_Option('Driver', 'chromedriver.exe') _WD_Option('Port', 9515) _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"') $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}' EndFunc ;==>SetupChrome  
      test.html test.xlsx
    • By kawumm3000
      Hi @water
      in my excel file it doesn't work because it counts also all the cells that a somehow formated. Can this also ignores formated cells and only counts cells with data?
      thanks
    • By dsm-sas
      Hi,
      I am looking for the possibility to extract a xls (not xlsx)-Excelfile to a csv . My problem: I do NOT have any Appliaction from MS-OFFICE. All searchings (since 3 days now) allways wants me to install ms-office or excel.
      I CAN'T DO THIS !!!
      Any help?!
      Ths'x alot
    • By Cristin
      Dear all,
      first of all hi.
      I`m in trouble, again 😕
      I can`t find right solution, tried all possible combinations generated by my mind and nothing.
      I need to copy range of values depending by values from two different cells in excel sheet.
      I put an example in attachment.

      I have two values which is never changed (is changed only TEXT). I need to copy all the rows from VALUE 1 to VALUE 2 in new Sheets (Output 1, Output 2, ... , Output X).
      It is possible to do this using Autoit? 🤔
      Also in attachment you can find last example which I have tried and it`s something near, it is copying by _Excel_RangeFind row with needed VALUE 1 but I need to copy whole range from VALUE 1 to VALUE 2 😵 ( see in attachment examples).
      Thank you very much, in advance, for your support, solution really will make my life much easier 🙃
      Have a nice day all of you!
      Best Regards,
      Cristin
      Book1.xlsx something.au3
×
×
  • Create New...