Jump to content

Recommended Posts

Hello Autoit community

I started using Autoit about 2 weeks ago and have found myself lost looking for information. Majority of the Help options are very easy to understand but I think that my general lack of knowledge about programming has slowed my progress. I tried very hard to not be that guy asking annoying questions on the forum but I’m afraid I’m out of options…

The steps below are my attempt to explain my scenario

Step 1: open excel file

Step 2: Read range of Excel column “A1” – “A???”) The range of rows will vary from sheet to sheet.

Step 3: take the values (one cell at a time) and place one value into my web browser ran program.

Step 4: if that value has been added before (indicated by the PixelSearch) than delete (represented by the mouse click) and start the loop over moving onto the next cell.

Step 5: if that value is new then submit the value (represented by the 2 mouse clicks at the end of the (if function)

The code listed below is as far as I got. I hope this explanation will suffice and I look forward to any help I can get.

Capture.JPG.e97e00fae45cf0dddc5e50b663d0

(From WinActive(“Paste Too”) down is working as intended.)

 

Share this post


Link to post
Share on other sites

You have a lot going on there ... I will give some high level feedback without re-writing your code.  First problem, the range of used cells in Excel varies.  To solve that problem you can use this:

$oRange = $oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeLastCell)

The next issue is the for loop.  I think you would want to read the cells into an array first using _Excel_RangeRead.   Once in the array, you can look for unique values using _ArrayUnique.  Then, after you have a unique array of values from your spreadsheet you can send them to your website in the for loop (your current approach goes for 10,000 times cell by cell every time - this would be much more efficient).  That would, based on your explanation, also eliminate the pixelsearch approach to finding duplicates after you send them (because there would be none before you send).

Hope some of that helps.

 

Share this post


Link to post
Share on other sites

There are better ways to automate a website, with IE.au3 is delivered with AutoIt. I suggest use _IEDocInsertHTML, this is the better/safer solution.

Edit: it's only temporary.

Edited by AutoBert

Share this post


Link to post
Share on other sites

Thank you for the feedback. I will look into your suggestions and update with new information. 

As for the PixelSearch elimination. I receive a new worksheet every week that i will run through this code so since ArrayUnique cannot sort the data that was placed into my website from previous worksheets it will be need to prevent future repetitive inputs. Sorry for not clearing that up in my original explanation. 

Thank you again jfish.

Share this post


Link to post
Share on other sites

You can also read Table from the webpage:

Quote

_IETableWriteToArray

Reads the contents of a Table into an array

Edited post#3:

1 hour ago, AutoBert said:

it's only temporary.

Look for _IEFormElementSetValue() and _IEFormSubmit.

Edited by AutoBert

Share this post


Link to post
Share on other sites

Hi @BrandonSanchez
Like @Jfish said you can do it in that way and about his high level recommendation I cant make that work and I end up with:

$aResult = _Excel_RangeRead($oWorkbook, Default,$oWorkbook.ActiveSheet.UsedRange.SpecialCells($xlCellTypeLastCell), 1)

But that only will give you last used cell on the entire Worksheet and not per row.

If the number of row will vary you can read the entire worksheet to an array with this code and then play with that array:

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

Local $oExcel = _Excel_Open()
If @error Then
    MsgBox(0, "Error", "Error creating Excel object")
    _Excel_Close($oExcel)
    Exit
EndIf
 Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Excel1.xlsx", Default, Default)
 If @error Then
     MsgBox(0, "Error", "Error opening the workbook")
     _Excel_Close($oExcel)
     Exit
 EndIf
 Local $aResult = _Excel_RangeRead($oWorkbook, Default, Default, 1)
 If @error Then
     MsgBox(0, "Error", "Unable to read workbook")
     _Excel_BookClose($oWorkbook)
     Exit
 EndIf
 _ArrayDisplay($aResult, "Data", "")
 _Excel_BookClose($oWorkbook)

Note that this is not 1D array so you will need to make some test with _ArrayUnique() because the way to do it is not the same.

Regards
Alien.

Share this post


Link to post
Share on other sites

@alien4u I did not know the worksheet had more than one used column since the OP related to Col A onl but you should be able to swap out a specific range for

.UsedRange

And I think it would work for any given column instead of all of them.  

Share this post


Link to post
Share on other sites
5 hours ago, Jfish said:

@alien4u I did not know the worksheet had more than one used column since the OP related to Col A onl but you should be able to swap out a specific range for

.UsedRange

And I think it would work for any given column instead of all of them.  

Hi @Jfish I tried that before and I cant find a way to use that per row or per columns, you can take a look to this:


If you know a way to do it or you could find I way will be very nice.
Thanks you.

Regards
Alien.

Share this post


Link to post
Share on other sites

This post explains how to do it for a single column:

This is a working example:

#include<Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\testdata.xlsx") ; your workbook here

Global Const $xlUp = -4162  ;global constant for Excel up, finding last row (thanks Water ... pulled from your post)
With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "B").End($xlUp).row ; switch out your cols here
EndWith
MsgBox(0,"",$lastRow)

@water I did not notice $xlUp in the excel constants.  I know you could probably fill that file to a crazy size if you added everything but wondering if would consider adding it?

Edited by Jfish

Share this post


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

This post explains how to do it for a single column:

This is a working example:

#include<Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\testdata.xlsx") ; your workbook here

Global Const $xlUp = -4162  ;global constant for Excel up, finding last row (thanks Water ... pulled from your post)
With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "B").End($xlUp).row ; switch out your cols here
EndWith
MsgBox(0,"",$lastRow)

@water I did not notice $xlUp in the excel constants.  I know you could probably fill that file to a crazy size if you added everything but wondering if would consider adding it?

Hi @Jfish and thanks you again but I'm still unable to make that work per row and not per columns.
With this:

With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "B").End($xlUp).row ; switch out your cols here
EndWith

I'm able to get how much rows per columns or how much columns from A to H with this:

With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Columns.Count, "H").End($xlUp).column ; switch out your cols here
EndWith

I also try this to get last value per column but I still need last used cell per row, notice is not the same last used cell(last cell with a value) so I dont' need last cell per column or last cell per row, I need last used cell per row.

Global Const $xlUp = -4162  ;global constant for Excel up, finding last row
$LNC = $oWorkbook.ActiveSheet.Range("F65535").End($xlUp)
$sResult = _Excel_RangeRead($oWorkbook, Default, $LNC, 1)

Regards
Alien.
 

Share this post


Link to post
Share on other sites

The code I supplied was last used row in a col.  This is last used col in a row

;Find the last used column in a Row: row 1 in this example
Global Const $xlToLeft = -4159
With $oWorkbook.ActiveSheet
    $lastCol = .Cells(2, .Columns.Count).End($xlToLeft).Column; where '2' is the row ... 
EndWith
MsgBox("","",$LastCol)

I am not 100% sure I follow what you are trying to do but does that get you there?

Edited by Jfish

Share this post


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

The code I supplied was last used row in a col.  This is last used col in a row

;Find the last used column in a Row: row 1 in this example
Global Const $xlToLeft = -4159
With $oWorkbook.ActiveSheet
    $lastCol = .Cells(2, .Columns.Count).End($xlToLeft).Column; where '2' is the row ... 
EndWith
MsgBox("","",$LastCol)

I am not 100% sure I follow what you are trying to do but does that get you there?

Thank you again, I was able to understand and get that.
What I want is last used cell in a row...
I mean:
- In Row 1 last used cell is: F1(get data there)
- In Row 2 last used cell is: G1(get data there)
And get all this values from last used cell for in a row(row by row) of an entire Worksheet.

Regards
Alien.
 

Share this post


Link to post
Share on other sites

Okay, now I think I understand ... how about this (selects cell in question):

;Find the very last used cell in a Row:
$rowNum=3; this is the row number you are targeting to find the last used cell
With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$rowNum).End($xlToLeft).Select
EndWith

This one returns the specific address:

;Find the very last used cell in a Row:
$rowNum=3; this is the row number you are targeting to find the last used cell
With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$rowNum).End($xlToLeft).Address
EndWith
MsgBox("","",$LastUsedCellInRow)

 

Edited by Jfish

Share this post


Link to post
Share on other sites

Hi again @Jfish
Thanks to your help I figure it out and end with this code:

#include<Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\Excel1.xlsx") ; your workbook here
Global Const $xlUp = -4162
Global Const $xlToLeft = -4159
;Number of Rows
With $oWorkbook.ActiveSheet
     $countRow = .Cells(.Rows.Count, "A").End($xlUp).row
EndWith
For $i = 1 To $countRow
    ; Number of Columns in 1 Row
    With $oWorkbook.ActiveSheet
        $countCol = .Cells($i, .Columns.Count).End($xlToLeft).Column
    EndWith
    $ColLetter = _Excel_ColumnToLetter($countCol)
    $aResult = _Excel_RangeRead($oWorkbook, Default,$ColLetter&$i , 1)
MsgBox(0,"",$aResult)
Next

And I re use your last code like this:

#include <Excel.au3>
Local $oAppl = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oAppl,@ScriptDir&"\Excel1.xlsx") ; your workbook here
Global Const $xlUp = -4162
Global Const $xlToLeft = -4159
;Number of Rows
With $oWorkbook.ActiveSheet
     $countRow = .Cells(.Rows.Count, "A").End($xlUp).row
EndWith
For $i = 1 To $countRow
    ; Number of Columns in 1 Row
    With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$i).End($xlToLeft).Address
EndWith
MsgBox("","",$LastUsedCellInRow)
Next

But the return value is "$$column$$row" literally like this so that give me an extra $ sign, example:
"$C$3"(real value return) but it should be "C3"

Anyways I like this code more than the first one.

Thanks you so much.

Regards
Alien.

Share this post


Link to post
Share on other sites

This will remove the extra dollar signs ...

$rowNum=3; this is the row number you are targeting to find the last used cell
With $oWorkbook.ActiveSheet
    $LastUsedCellInRow = .Range("IV"&$rowNum).End($xlToLeft).Address
    $LastUsedCellInRow = StringReplace($LastUsedCellInRow,"$","")
EndWith
MsgBox("","",$LastUsedCellInRow)

 

Share this post


Link to post
Share on other sites

@Jfish I have tried reading the help and after reading your continued conversation with Alien4u this is what I came up with.

Capture3.JPG.65557567e7bb5e65198d140cae6

The good news is I get no errors but the bad news is that it doesn’t work.

I am getting it to open the worksheet and copy but its only copies 0 from somewhere and paste 0 into notebook but the cell values have names. Any suggestions because the reading material isn’t that easy for me to understand. Also, I’m 100% sure I have the For loop wrong

Share this post


Link to post
Share on other sites

@Jfish & @alien4u 

(Please ignore post #16 Jfish i added more below)

 

I looked at your examples, read the help menu, and looked at other people's posts on similar issues to try and get a grasp of what is going on and this is what i was able to come up with. I get no errors when running this but i also get no results. 

Capture4.JPG.30c2b0d1efc10080d6b6ea85d28

 

I really appreciate both of your help.

 

Share this post


Link to post
Share on other sites

Could you please use the code tags <> when posting?  It makes it easier on our side.  That said $lastRow is only giving the a numeric value of the last row.  Therefore your range argument in _Excel_RangeRead is a single row number (when it should be a range). Try this instead:

$aResult = _Excel_RangeRead($oWorkbook,Default,"A1:A"&$lastRow)
_ArrayDisplay($aResult)

EDIT: I would also add some debugging.  When you are not sure where your errors are (this was just one - I did not study the whole thing) then best to test results as you step through the code with consolewrite or msgbox to see if you are getting the values you expect along the way.

Edited by Jfish

Share this post


Link to post
Share on other sites

I believe I'm making major progress @Jfish but I have 1 last issue that i'm stumped on.

Global Const $xlUp = -4162
With $oWorkbook.ActiveSheet
     $lastRow = .Cells(.Rows.Count, "A").End($xlUp).row
  EndWith
Local $aResult = _Excel_RangeRead($oWorkbook,Default,"A1:A"&$lastRow)
For $i = 0 to $lastRow
   _ArrayToClip($aResult,"", Default,0, $lastRow)

This code gets my array accurately (See below pic) but the last issue is when the loop restarts it keeps pasting the start of the _ArrayToClip which is "Test1"of the array, does the _ArrayToClip need to be its own loop inside of the $i loop? 

 571afbe77556a_ArrayDisplay.JPG.2b194967d  

Share this post


Link to post
Share on other sites

That is because you are starting your loop at 0 and "Test1" is in the 0 index of the array.  Try starting your for loop at 1 like this: for $i=1 to $lastrow.  Then the first value you will pass would be 2 in the example above.

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

    • By Taxyo
      Hi,
       
      I've been trying to automate modification of an excel file and the last thing I am stuck on is deleting all the rows where the value of Column 13 is 0. 
      I believe the error is due to me not fully understanding the syntax so this is where I'm stuck: 
       
      Func Hotkey2() Global $aUsedRange = _Excel_RangeRead($oWorkbook, 1) _ArrayDisplay($aUsedRange) For $iRow = UBound($aUsedRange) - 1 to 3 Step -1 If $aUsedRange[$iRow][13] = 0 Then _Excel_RangeDelete($oWorkbook.Worksheets(1), $aUsedRange[$iRow] & ":" & $aUsedRange[$iRow], default, 1) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeDelete Example 2", "Error deleting rows." & @CRLF & "@error = " & @error & ", @extended = " & @extended) EndIf Next EndFunc  
      While my script properly locates the row which contains value 0 in Column 13, I am not sure how to set it to the corresponding row in the excel workbook?  My above experiment gives me $vRange error and I've been toying around with it to no avail. The only way I get the Script to delete a row is by actually specifying "4:4" or "6:8" etc. 
      Where am I going wrong?
       
      Thanks! 
    • By Most
      #include <Array.au3> #include <Excel.au3> #include <MsgBoxConstants.au3> ; Create application object and open an example workbook Local $oExcel = _Excel_Open() If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\trans.xlsx") If @error Then MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example", "Error opening workbook '" & @ScriptDir & "\trans.xlsx'." & @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, "A1") If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 1", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example 1", "Data successfully read." & @CRLF & "Value of cell A1: " & $sResult) Hi, all.
      Ok, here is the deal. I have simple excel file called trans.xlsx. It's located in the directory of script. In general i don't care where to store it. 
      What i do need is to open excel file and copy one by one numbers from cells. I've tried different ways, examples. But i only get error, says: error = 3, extended = 1. I saw different posts from different years. I even tried to use simple example from manual file. But always get error.

      In general my goal get numbers one by one and post it to let's say search filed in my PC one by one. Or to notepad (but one by one, in kind of loop). 
      I've learned how to copy or show in message box some info from other apps. But with excel i'm stuck. 

      I'm able to open needed window based on "title" of excel. But i don't succeed of copying info from cells. 

      Would be appreciate for any help. 
      So, in this code i'm trying at least to read from cell A1. Doesn't matter what Sheet. 

      I use Windows 10, Excel for Office 365. 
      Thank you in advance. 
    • By VinMe
      Dear all, i am unable to open a xml file to excel in the "xml table format" Please help me out in where i am missing
      Local $strFileToOpen = _WinAPI_OpenFileDlg('Select xml file', @WorkingDir, 'All Files(*.*)', 1, '', '', BitOR($OFN_PATHMUSTEXIST, $OFN_FILEMUSTEXIST, $OFN_HIDEREADONLY)) Global $xlXmlLoadImportToList = 2 ; Places the contents of the XML data file in an XML table $oExcel = _Excel_Open() $oWorkbook1=$oExcel.Workbooks.OpenXML($strFileToOpen, "", $xlXmlLoadImportToList) If $strFileToOpen <> False Then     Local $oWorkbook1 = _Excel_BookOpen($oExcel, $strFileToOpen) EndIf Error i am getting is:
      ......\81e_Compare_v1.au3" (46) : ==> The requested action with this object has failed.:
      $oWorkbook1=$oExcel.Workbooks.OpenXML($strFileToOpen, "", $xlXmlLoadImportToList)
      $oWorkbook1=$oExcel.Workbooks^ ERROR
      >Exit code: 1    Time: 7.338
    • By VinMe
      Dear all, 
      I am unable to get the right result after applying the filter to the excel. please let me know on the same.
      issue: After applying the filter the output $lastRow11 not giving the right output of complete visible rows. (its breaking at row skips)
       
      ;DATA EXTRACTION FROM LOC EXCEL
      ;=============================================================================
      $oWorkbook = _Excel_BookAttach($sWorkbook)
      Local $sMSN = InputBox("MSN NO", "Enter MSN in XX FORMAT", "")
      ;~ Local $LastRow1 = ($oWorkbook.ACTIVESHEET.Range("A1").SpecialCells($xlCellTypeLastCell).Row)
      $LastRow1 = $oWorkbook.ActiveSheet.UsedRange.Rows.Count
      MsgBox(0, "lastrow1", $LastRow1)
      _Excel_FilterSet($oWorkbook, $oWorkbook.activesheet, "AF1", 32, "*" & $sMSN & "*")
      Local $oLocDS = $oWorkbook.ActiveSheet.Range("S1:S" & $LastRow1).SpecialCells($xlCellTypeVisible)
      Local $LastRow11 = $oLocDS.rows.count    ;error output
      MsgBox(0, "lastrow11", $LastRow11)
      Local $aLocDS1 = _Excel_RangeRead($oWorkbook, Default, $oLocDS)
      Local $oLocNr = $oWorkbook.ActiveSheet.Range("A1:A" & $LastRow1).SpecialCells($xlCellTypeVisible)
      Local $aLocNr1 = _Excel_RangeRead($oWorkbook, Default, $oLocNr)
      _ArrayDisplay($aLocDS1)
      _ArrayDisplay($aLocNr1)
      _ArrayTrim($aLocDS1, 6, 1)
      _ArrayTrim($aLocNr1, 6, 1)
      _ArrayTrim($aLocNr1, 6, 0)
      _ArrayDisplay($aLocDS1)
      _ArrayDisplay($aLocNr1)
    • By VinMe
      I am unable to execute the below script, my requirement is to copy the content from active excel sheet and to display the same.
      Please let me know where i am missing!
      #include <Excel.au3>
      #include <MsgBoxConstants.au3>
      #include <Array.au3>
      #include <StringConstants.au3>
      Local $oExcel = _Excel_Open()
      $LastRow2 = $oExcel.UsedRange.Rows.Count
      $Tissue = _Excel_RangeRead($oExcel, Default, "E1:E" & $LastRow2)
      $TshNr = _Excel_RangeRead($oExcel, Default, "F1:F" & $LastRow2)
      _ArrayDisplay($Tissue)
      _ArrayDisplay($TshNr)
×
×
  • Create New...