Jump to content
BrandonSanchez

Incremental copy paste

Recommended Posts

BrandonSanchez

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
Jfish

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
AutoBert

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
BrandonSanchez

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
AutoBert

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
alien4u

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
Jfish

@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
alien4u
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
Jfish

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
alien4u
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
Jfish

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
alien4u
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
Jfish

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
  • Like 1

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
alien4u

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
Jfish

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
BrandonSanchez

@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
BrandonSanchez

@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
Jfish

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
BrandonSanchez

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
Jfish

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

    • Ahmed101
      By Ahmed101
      I have more than 12 workbooks opened together, if i wanted to attach to the last workbook opened it will take more than 1 minute !
      Is there any solution for that ?
    • BogdanNicolescu
      By BogdanNicolescu
      While 1 / / / A whole bunch of codes found in help and here: https://www.autoitscript.com/autoit3/docs/ / / / WEnd OR:

       
      HotKeySet("{ESC}", "Terminate") While 1 MouseClick("Right",674,422) MouseClick("Left",673,447) Sleep(2000) / / / A whole bunch of codes found in help and here: https://www.autoitscript.com/autoit3/docs/ / / / MouseClick("Left",675,339) SLeep(3000) WEnd Func Terminate()     Exit 0 EndFunc  
      Sorry if i should't let this here to be found by newbies like me -.-'
    • Daniza
      By Daniza
      Hello! where should I start, if I want to have a Progress Bar while waiting for my File to be open, can I use WinWaitActive? Thanks,
    • Evolutionnext
      By Evolutionnext
      I am still a noob and not a programmer, would greatly appreciate your help.
       
      Task:
      Open Excel file with file path and name: C:\Users\GENOBEAUTYPC1\Desktop\ACTIVE BEAUTY LABELS\BeautyMe Label 200ml ACTIVE VERSION.xlsx
      This file path and name is saved in the variable: $sAnswer
      Go to Excel Tab called "formular"
      Go to Cell A1
      Insert the text saved in the variable: $sAnswer2
      ATTENTION!!! This has 2 problems.
      Problem number 1: This text contains special characters that need to be interpreted as raw text. (content is: Gemischt für#30 ml#Mindestens haltbar bis#Maria Wallerstorfer#Anwendung: Täglich 1x morgens auf das gereinigte Gesicht auftragen. Augenkontakt vermeiden.#Über 0 C° und unter 25 C° lagern.#Lot:N8A1028/D30/V2.1#Genome Plus GmbH#Georg-Wrede-St. 13, D-83395 Freilassing#GEN SERUM#DAY)
      Problem number 2: This textis longer than 255 characters.
       
      Can anyone help me?
       
      I try to do it really primitively by opening the excel, waiting until it is open, clicking where the tab is, clicking where the cell is and inserting the content of the variable, but I am stuck at the point where I am limited by 255 characters.
       

      ; Opening the right excel FileChangeDir
                  tooltip("File exists and is called:"&$sAnswer ,300,300)
                  ShellExecute($sAnswer ,"" ,"" ,"" , @SW_MAXIMIZE)
                  sleep(7000)
                  
                  tooltip("Now lets insert the right content into the excel",300,300)
                  MouseClick("left",226,1004)
                  MouseClick("left",52,179)
                  sleep(500)
                  Send("A1")
                  sleep(500)
                  send("{enter}")
                              tooltip("inserting label content",300,300)
                  sleep(500)
                  Send($sAnswer2,  1)
                                          tooltip("inserting INCIS",300,300)
                  sleep(5000)
                  Send($sAnswer3, 1)
                  sleep(5000)
       
       
       
    • AzgarD
      By AzgarD
      Hi guys. I know this is a newbie topic, very newbie, but i've read a lot of stuff and still don't get it. I just need to copy something from Excel cell, paste this in other program, copy something in this program and paste in other Excel cell. Something like...
      Copy A2 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C2 Then Copy A3 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C3 ... And it goes on The problem is, how can i "communicate" with Excel and do this row change? Like A2 to C2 and A3 to C3 ... In a efficient way that can be done like hundreds of times.
      Very newbie question but still not understanding this.
       
      Ty guys.
×