BrandonSanchez

Incremental copy paste

24 posts in this topic

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.

 


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

#5 ·  Posted (edited)

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.  


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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

#9 ·  Posted (edited)

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

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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

#11 ·  Posted (edited)

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

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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

#13 ·  Posted (edited)

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
1 person likes this

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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)

 


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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

#18 ·  Posted (edited)

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

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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.


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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

    • 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?
       
    • AndreyS
      By AndreyS
      Hello to you, dear developers!
      Could you please tell me if there is an easy way to determine if the information was pasted from the clipboard?
      Without any variation of the insertion via Ctrl+V, or through the context menu.
      I revised the Help and many forum topics, but found nothing suitable.
      Can there be any WinAPI system events that determine this event?
    • 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.
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to write a value into an excel column.
      I know where it starts from, but i don't know what the end is, last non-empty cell.
      How can i get the number of last non-empty cell?
      Thanks in advance.
      Regards