padinski

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
;-----------------------------------------------------------------------------
$p=($c-1)/$rows*100
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
ContinueLoop
EndIf
_Excel_RangeCopyPaste($oWorkbook2.Worksheets(1), "AM:AN")
_Excel_FilterSet($oWorkbook, Default, Default, 2, ($aArray1[$c][1]))
_Excel_RangeCopyPaste($oWorkbook.Activesheet, Default, "P", Default, $xlPasteValues)
next

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:

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

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

    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have an error:
      ==> The requested action with this object has failed.: $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count $iRowCount = .Range(^ ERROR  
      My code is:
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) Sleep(1000) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count Sleep(1000) _Excel_RangeWrite($oWorkbook_1, $oWorkbook_1.ActiveSheet,$aFileList[$i][2] , "AB3:AB"&$iRowCount) I have added some sleep because the application was crashing more often before, so i thought to slow down the code execution.
      But i didn't solve the issue.
      Has anyone an idea of what the problem might be?
      Thanks in advance.
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Written by GreenCan and water.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have a problem with the deletion of an empty row in Excel.
      My code:
       
      If $vRow_2 = "" Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf I want to delete the second row. $vRow_2 is an empty cell, "A2".
      After running the code, the second row is not deleted.
      I have tried also:
       
      If $vRow_2 = Null Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf But it doesn't work.
      Any suggestion?
      Thanks in advance.
    • 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?