Compare 2 excel spreadsheets if amatch is found copy certain cells back the original workbook

4 posts in this topic

#1 ·  Posted (edited)

Hi all,

As the title suggests. I have 2 spreadsheets with different information in both, however their is 1 common value to link the 2 spreadsheets together.

What I want to do is have everything from column B in an array from workbook1, the script would loop through the array searching for that exact value in workbook2 in column A. If that value is found in workbook2, I need to copy information from 2 different cells that relate to that record from workbook2 back into workbook1.

This is what I have so far but, just can't get the copying it the cell values from workbook2 into woorkbook 1.

; Select filepath with message to display in FileOpenDialog.
Local Const $sMessage = "Select workbook filepath."
Local $sFilePath = FileOpenDialog($sMessage, "D:\Users\xxxxx\Documents\", "Excel (*.xls;*.xlsx;*.csv)|", $FD_FILEMUSTEXIST)
; Create application object or connect to an already running Excel instance
Local $oAppl = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF:", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;open workbook1
$oWorkbook = _Excel_BookOpen($oAppl, $sFilepath, Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF:", "Error opening '" & $oWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Select filepath with message to display in FileOpenDialog.
Local Const $sMessage2 = "Select workbook2 filepath."
Local $sFilePath2 = FileOpenDialog($sMessage2, "D:\Users\xxxxx\Documents\", "Excel (*.xls;*.xlsx;*.csv)|", $FD_FILEMUSTEXIST)
;open workbook2
$oWorkbook2 = _Excel_BookOpen($oAppl, $sFilepath2, Default, Default, True)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF:", "Error opening '" & $oWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;Read array from workbook
Local $aArray1 = _Excel_RangeRead($oWorkbook, Default)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF:", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Turn on progress GUI
ProgressOn("Progress", "data washing progress", "0%")
; counts the number of rows in the array
$rows = UBound($aArray1) -1
; script start
for $c = 1 to $rows
; Progress calcualtions
ProgressSet($p,$c-1&" of "&$rows&" records processed")
; Find all id's as per value in column B from workbook in workbook2
_Excel_FilterSet($oWorkbook2, Default, Default, 1, ($aArray1[$c][1]))
If @error Then
_Excel_RangeCopyPaste($oWorkbook2.Worksheets(1), "AM:AN")
_Excel_FilterSet($oWorkbook, Default, Default, 2, ($aArray1[$c][1]))
_Excel_RangeCopyPaste($oWorkbook.Activesheet, Default, "P", Default, $xlPasteValues)

Anyone able to see where I have gone wrong or can suggest a better approach to do this?

Edited by padinski

Share this post

Link to post
Share on other sites

Do you get an error or does it just not give the desired result?

My UDFs and Tutorials:


Active Directory (NEW 2017-04-18 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
ADO - Wiki


Share this post

Link to post
Share on other sites

Why do you write this in autoit and not in vba macro. Much easier and much quicker.



Share this post

Link to post
Share on other sites

thanks for taking a look guys, just not the desired result Water.
Junkew good point.

FYI:  I ended up just combining the two workbooks into one and having the data on 2 worksheets, just using the following array formula in excel:

In cell P2 in Sheet1
=INDEX('Sheet2'!AM:AM, MATCH(B2, 'Sheet2'!A:A, 0))

In cell Q2 in Sheet1
=INDEX('Sheet2'!AN:AN, MATCH(B2, 'Sheet2'!A:A, 0))

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

    • SkysLastChance
      By SkysLastChance
      Is there an easy way to format a cell to date or currency? I found some old threads that were not much help. 
      I have looked at the wiki but I only see how to format as a number
      I assume there is not a way like I did for making the text bold. 
      Any suggest or help would be appreciated. 
      I am able to get the format changed to text using
      $oExcel.Activesheet.range("A1:L1").NumberFormat = "@"  
      #include <Excel.au3> Global $sBox Func InputHowMany() While 1 $sBox = Number(InputBox("How many?", "How many?")) If $sBox = 0 Then $iMsg = MsgBox(1, 'Oops', 'Please enter a valid number') If $iMsg = 2 Then Exit Else Return $sBox - 1 EndIf WEnd EndFunc InputHowMany() $oExcel = _Excel_Open() $oExcel = _Excel_BookNew($oExcel) Local $t = 2 Local $w = 1 Local $c = 301 $oExcel.Activesheet.range("A1:L1").font.bold = True Do _Excel_RangeWrite($oExcel,Default, "Status", "A1") _Excel_RangeWrite($oExcel,Default, "Last Name", "B1") _Excel_RangeWrite($oExcel,Default, "Last Name", "C1") _Excel_RangeWrite($oExcel,Default, "SSN", "D1") _Excel_RangeWrite($oExcel,Default, "DOB", "E1") _Excel_RangeWrite($oExcel,Default, "Email", "F1") _Excel_RangeWrite($oExcel,Default, "Mailing Address", "G1") _Excel_RangeWrite($oExcel,Default, "City", "H1") _Excel_RangeWrite($oExcel,Default, "State", "I1") _Excel_RangeWrite($oExcel,Default, "Zip Code", "J1") _Excel_RangeWrite($oExcel,Default, "Gender", "K1") _Excel_RangeWrite($oExcel,Default, "Phone", "L1") _Excel_RangeWrite($oExcel,Default, '=B' & $c, "B" & $t) _Excel_RangeWrite($oExcel,Default, '=C' & $c, "C" & $t) _Excel_RangeWrite($oExcel,Default, '=D' & $c, "D" & $t) _Excel_RangeWrite($oExcel,Default, '=E' & $c, "E" & $t) _Excel_RangeWrite($oExcel,Default, '=F' & $c, "F" & $t) _Excel_RangeWrite($oExcel,Default, '=G' & $c, "G" & $t) _Excel_RangeWrite($oExcel,Default, '=H' & $c, "H" & $t) _Excel_RangeWrite($oExcel,Default, '=I' & $c, "I" & $t) _Excel_RangeWrite($oExcel,Default, '=J' & $c, "J" & $t) _Excel_RangeWrite($oExcel,Default, '=K' & $c, "K" & $t) _Excel_RangeWrite($oExcel,Default, '=L' & $c, "L" & $t) $c = $c + 1 $t = $t + 1 $w = $w + 1 Until $w > $sBox  
    • JayHawkfl
      By JayHawkfl
      Super simple super quick script I made to randomize teams for mixed doubles. Haven't coded for a while so it felt good to write anything. 
      #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <SliderConstants.au3> #include <WindowsConstants.au3> #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> Local $arraySize = 2 local $array1[50] = ["Doug","Julio"] local $array2[50] = ["Hannah","Sheila"] Local $oBook #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Doubles Team Mixer", 615, 438, 192, 124) $Bachround = GUICtrlCreatePic("C:\Users\a607150\Pictures\2.jpg",0,0,615,438) $Input1 = GUICtrlCreateInput("", 64, 128, 121, 175) $Input2 = GUICtrlCreateInput("", 364, 128, 121, 175) $Button1 = GUICtrlCreateButton("Serve",216,140,121,42) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $array1 = GUICtrlRead($input1) $array1 = StringSplit($array1,",") $array2 = GUICtrlRead($input2) $array2 = StringSplit($array2,",") _ArrayDelete($array1,0) _ArrayDelete($array2,0) _ArrayShuffle($array2) $oBook = _Excel_Open(Default, Default, Default, Default, True) $oWorkBook = _Excel_BookNew($oBook, 1) _Excel_RangeWrite($oWorkBook,$oWorkBook.activesheet,"Guys","A1") _Excel_RangeWrite($oWorkBook,$oWorkBook.activesheet,"Gals","B1") _Excel_RangeWrite($oWorkBook,$oWorkBook.activesheet,$array1,"A2") If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite Example 2", "Error writing $array1 to worksheet." & @CRLF & "@error = " & @error & ", @extended = " & @extended) _Excel_RangeWrite($oWorkBook,$oWorkBook.activesheet,$array2,"B2") EndSwitch WEnd  
    • jjohn
      By jjohn
      Hi all,
      I have the following code, basically, it populates an array with unique characters found in a string, but it turns out only one character is in the array, i don't want to think it is due to a bug of autoit at this point, so i need your help on why, please
      Dim $sx[0] Dim $cn $ss = "Xx" ConsoleWrite(StringLeft($ss, 1) & " " & StringRight($ss, 1) & @CRLF) ConsoleWrite(StringLeft($ss, 1) = StringRight($ss, 1) & @CRLF) ;here show X <> x ConsoleWrite(@CRLF) ;the following is to populate array $sx with different unique characters in $ss For $b = 1 To StringLen($ss) $flg = 0 For $a = 0 To UBound($sx) - 1 ;if asc(StringMid($ss,$b,1)) = asc($sx[$a]) Then ;if this line is used instead of the next, everything is fine If StringMid($ss, $b, 1) = $sx[$a] Then If StringMid($ss, $b, 1) = "x" Then ConsoleWrite("<<->>" & StringMid($ss, $b, 1) & " " & $sx[$a] & @CRLF) $flg = 1 ExitLoop EndIf Next If $flg = 0 Then $cn = $cn + 1 ReDim $sx[$cn] $sx[$cn - 1] = StringMid($ss, $b, 1) EndIf Next ConsoleWrite(UBound($sx) & @CRLF);list the ubound of $sx ;list what is in array $sx For $a = 0 To UBound($sx) - 1 ConsoleWrite($sx[$a] & @CRLF) Next same code in a file is attached as uniqueChar2Array.au3
    • singbass
      By singbass
      I have an issue with disk space on a server so I wrote a simple little script to check specific directories and save the sizes to an Excel spreadsheet.  For this script, I am still using version  Everything works fine, I just have a question.
      #cs ---------------------------------------------------------------------------- AutoIt Version: #ce ---------------------------------------------------------------------------- #include<date.au3> #include<excel.au3> #include<array.au3> $file = FileOpen(@ScriptDir & "\CMScriptDirList.txt", 0) If @error Then Exit ;--folder view still OK $oexcel = _ExcelBookOpen(@ScriptDir & "\CMScriptDirSizesCopy.xlsx") If @error Then Exit ;--folder view now parent folder $excelArray = _ExcelReadSheetToArray($oexcel) $lastrow = $excelArray[0][0] $lastcol = $excelArray[0][1] _ExcelWriteCell($oexcel, _NowCalc(), 1, $lastcol + 1) While 1 $line = FileReadLine($file) If @error Then ExitLoop $size = DirGetSize(StringStripWS($line, 3)) / 1024 / 1024 $iIndex = _ArraySearch($excelArray, $line, 0, 0, 0, 0, 1, 1) If @error Then ContinueLoop _ExcelWriteCell($oexcel, $size, $iIndex, $lastcol + 1) WEnd FileClose($file) _ExcelWriteFormula($oexcel, "=SUM(R2C" & $lastcol + 1 & ":R38C" & $lastcol + 1 & ")", 39, $lastcol + 1) _ExcelWriteFormula($oexcel, "=R39C" & $lastcol + 1 & "/1024", 40, $lastcol + 1) $oexcel.ActiveSheet.columns($lastcol).copy ;used to copy the format of the original last column of the spreadsheet $oexcel.ActiveSheet.columns($lastcol + 1).PasteSpecial(-4122, Default, Default, Default) ;this just pastes the format of the original last column to the new last column $oexcel.ActiveSheet.Range("A1").Select ;select cell A1 just to unselect the entire column from previous command $oexcel.columns.AutoFit ;auto sizes the column width _ExcelBookClose($oexcel, 1) ;save file when closing The script is compiled and sitting is a sub-directory on the server in question. The text file and the spreadsheet that are used are both in this same folder as well.  When I navigate to the folder and run the script by double-clicking on the executable, the process runs but the folder view where I ran the script will go back up one level so when the script completes, I am in the parent folder from where I started.  I have added message boxes throughout the script and have determined that the folder view goes back up one level at some point after the @error check for the file open and before the @error check for the ExcelBookOpen (where the comments are).
      I just wanted to know if someone can tell me why and if there is a way to prevent it. (Note: still using v3.3.8.1 on this machine but slowly converting scripts to v3.3.14.2).
    • KimberlyJillPereira
      By KimberlyJillPereira
      I am a newbie in AutoIt. May I know what is the code used to expand the width and height of excel cells because I want to insert pictures in the cell.I tried AutoFit but that doesnt work as I cant specify for the width and height. Only for column width I could expand by using .ColumnWidth but for the row I am not able to expand the row? How to do?? What is the code used ??Please help me and thank you.