SkysLastChance

[Solved] _Excel_RangeFind Questions

19 posts in this topic

#1 ·  Posted (edited)

My Main goal is to copy the info from sheet 2 and put it in the correct date and time location on sheet 1.

1.xlsm - Password 2007

2.xlsx

1. I do not understand why I am unable to find the date. I am reading the value and searching for the value. But, it never turns anything up.

2. What would be the best way (function) to go about selecting the second cell underneath the date after I have found it? 

So Ideally, I would want to match the date. Once it finds the date go 2 cells down insert the data from sheet 2. I believe I have a good idea on how to loop the script. The only thing I am not sure on is how to take the cell I find and get the cell location (exp. A2)  to go the cell that I find  and select 2 cells down and go to A4.

In other words I don't know how to put the cell I find into a variable that I can use to position where I start to input data. 

 

Any help is appreciated as always.

#include <Excel.au3>
#include <Array.au3>

MsgBox(0, "", "Open Excel 1")

Global $sExcelFile1 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsm)")

MsgBox(0, "", "Open Excel 2")

Global $sExcelFile2 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "Excel Sheet (*.xlsx;*.xls)|All (*.*)")


If FileExists($sExcelFile2) Then
   Global $oExcel2 = _Excel_Open ()
    $oExcel2 = _Excel_BookOpen($oExcel2,$sExcelFile2)
EndIF


If FileExists($sExcelFile1) Then
   Global $oExcel1 = _Excel_Open ()
    $oExcel1 = _Excel_BookOpen($oExcel1,$sExcelFile1,Default,Default,"2007")
EndIF


$oRead = _Excel_RangeRead ($oExcel2,Default,"A2")
$oRead1 = _Excel_RangeRead($oExcel1,Default,"BY4")
MsgBox  (0,"Test",$oRead)
MsgBox  (0,"Test",$oRead1)

$oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD4")

If @error Then
    MsgBox(4096, "", "No File(s) chosen")
Else
    Local $aSalesResult = _Excel_RangeFind($oExcel1, $oRead, Default, Default, $xlWhole)
    _Arraydisplay($aSalesResult, "RangeFind", 2, 0, "", "|", "Col|Sheet|Name|Cell|Value|Formula|Comment")
EndIf

 

Edited by SkysLastChance

Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites



#2 ·  Posted

@SkysLastChance how is this different than your other Excel thread, which water has already been active in? If it is a continuation of the same issue (as it appears to be) I will merge the topics.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted

@JLogan3o13I thought it would be better to start a new post because this has nothing to do with an error. Water solved my question; I just needed to update my UDF. I was not able to do any testing with _Excel_RangeRead. If you feel it is better merged by all means.  


Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#4 ·  Posted

If it is different that is fine - you simply seemed to be asking about getting the date in both threads.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#5 ·  Posted

The range you specify is invalid.

$oFind = _Excel_RangeFind ($oExcel1,$oRead,"=E4:FD4")

Use:

$oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD4")

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I must of done that when I was setting up the example. Thank you for catching this though. It is still not finding the date. 

Edited by SkysLastChance

Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#7 ·  Posted

This article explains why you need to use the displayed date for _Excel_RangeFind: http://www.ozgrid.com/VBA/find-dates.htm

So your example should look like:

#include <Excel.au3>
#include <Array.au3>

MsgBox(0, "", "Open Excel 1")

Global $sExcelFile1 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsm)")

MsgBox(0, "", "Open Excel 2")

Global $sExcelFile2 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "Excel Sheet (*.xlsx;*.xls)|All (*.*)")

If FileExists($sExcelFile2) Then
   Global $oExcel2 = _Excel_Open ()
    $oExcel2 = _Excel_BookOpen($oExcel2,$sExcelFile2)
EndIF

If FileExists($sExcelFile1) Then
   Global $oExcel1 = _Excel_Open ()
    $oExcel1 = _Excel_BookOpen($oExcel1,$sExcelFile1,Default,Default,"2007")
EndIF

$oRead = _Excel_RangeRead ($oExcel2,Default,"A2", 3) ; <== Line changed!!
$oRead1 = _Excel_RangeRead($oExcel1,Default,"BY4")
MsgBox  (0,"Test",$oRead)
MsgBox  (0,"Test",$oRead1)

$oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD4")

If @error Then
    MsgBox(4096, "", "No File(s) chosen")
Else
    Local $aSalesResult = _Excel_RangeFind($oExcel1, $oRead, Default, Default, $xlWhole)
    _Arraydisplay($aSalesResult, "RangeFind", 2, 0, "", "|", "Col|Sheet|Name|Cell|Value|Formula|Comment")
EndIf

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Thank you, Water

Dates in Excel are so complicated. XD

How do I display just the cell in a Msgbox?

 

Edited by SkysLastChance

Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#9 ·  Posted

Read the value of the cell using _Excel_RangeRead and display it ;) 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted

Sorry, I mean like displaying the cell, such as A1,B3,F5,etc.

or am I still able to accomplish this with _Excel_RangeRead? 

 

 

 


Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#11 ·  Posted

According to the help file the returned array contains the cell's address in element 2:

Quote
Success: a two-dimensional zero based array with the following information:
    0 - Name of the worksheet
    1 - Name of the cell
    2 - Address of the cell
    3 - Value of the cell
    4 - Formula of the cell
    5 - Comment of the cell

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

I keep getting a blank Msgbox.

How do I get a return  value with multiple success outcomes?

Do I have to grab it from the array?

 


Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#13 ·  Posted

Correct. As the help file describes you always get a 2D array from _Excel_RangeFind.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#14 ·  Posted

I really appreciate the help as always. 

Here is what I came up with.

$aArray1 = _ArrayToString($aSalesResult, " :: ", 0, 0, @CRLF, 2,2)

MsgBox(0, "Cell Address",$aArray1)

 


Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#15 ·  Posted

I suggest function _ArrayDisplay. 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#16 ·  Posted

Not sure what I am doing wrong. When I try it with _ArrayDisplay I just get a 1. 

$aArray = _Arraydisplay($aSalesResult,"Cell Address","0:0|2:2",64)

MsgBox(0, "Cell Address",$aArray)

 


Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#17 ·  Posted

8 minutes ago, SkysLastChance said:

When I try it with _ArrayDisplay I just get a 1. 

What were you expecting to see? Have you read the help file for the function?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I now understand why I am seeing a 1 

I guess my question would be is how do I pull the elements from _Arraydisplay?

Also why is _ArrayDisplay better then

$aArray1 = _ArrayToString($aSalesResult, "", 0, 0, "", 2,2)

also is "$Clip = _ArrayToClip($aSalesResult,"",0,0,"",2,2) just as bad as _ArrayToString?"

Edited by SkysLastChance

Life's simple. You make choices and you don't look back.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Each of the _Array* functions has a unique goal.
_ArrayDisplay displays the content of an array in readable format.
_ArrayToClip copies the content of an array to the clipboard.
So which function to use depends on what you want to achieve.

None of the functions is better or worse as another one.

To access an array within your script just use the row and column index as you would do in Excel.
To access the "cell" in the 2nd line and 4th column use

$sCell = $aSalesResult[1][3]

Why 1 and 3? Because the index for rows and columns of an array starts with 0.

For further information please check the array tutorial in the wiki.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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

  • Similar Content

    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi all,
      i have an empty csv file, i have a non formatted text file.
      What do i want to do?
      I want to automate the process "get external data" in Excel, i want to import the data from the text file and basically create a csv file with a specific character encoding.
      Is it possible with AutoIT?
       
    • breakbadsp
      By breakbadsp
      I  want to create a excel file from my script if it does not exist.
      _ExcelBookOpen throws error=2 if file does not exist, after this error i want to create new file at this point.
      can i use _FileCreate()?
      _Logger($sLogPath, "{INFO}------: Opening Excel File: " & $sExcelPath& "") While 1 Local $oExcelTestResult = _ExcelBookOpen($sExcelPath) If @error = 2 Then If not _FileCreate($sResExcelPath) Then MsgBox(0, "Error", "Error In Opening REsult Excel File: Error: " & String(@error)) _Logger($sLogPath, "{ERROR}------: Result Excel File does not exist.. tried to create new but :ERROR : " & String(@error) & "") ExitLoop Else _Logger($sLogPath, "{INFO}------: Result Excel File does not exist.. **Created New**: ") EndIf Else ExitLoop EndIf WEnd  
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to store an entire row of a Excel workbook into an array.  The row index is stored in a variable.
      How can i do it?
      Thanks in advance for your support.